Attempt to index nil with Position - lua

I'm trying to do something like a spear throw and I'm so confused. It says:
ServerScriptService.FireMagic.FireSpear:16: attempt to index nil with 'Position'
Anyways, here's the LocalScript code:
wait(1)
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Mouse = Player:GetMouse()
local rp = game:GetService("ReplicatedStorage")
local FireSpear = rp:WaitForChild("FireRemote")
local UIS = game:GetService("UserInputService")
local debounce = true
local cd = 10
UIS.InputBegan:Connect(function(input, isTyping)
if isTyping then
return
elseif input.KeyCode == Enum.KeyCode.E and debounce and Character then
debounce = false
FireSpear:FireServer()
wait(cd)
debounce = true
end
end)
and the Script:
wait(1)
local rp = game:GetService("ReplicatedStorage")
local ss = game:GetService("ServerStorage")
local Debris = game:GetService("Debris")
local ssFireSpear = ss.FireMagic:WaitForChild("ssFireSpear")
local FireRemote = rp:WaitForChild("FireRemote")
local UhTable = {}
local function LookatMouse(Mouse, RootPart)
local bodyG = Instance.new("BodyGyro")
bodyG.MaxTorque = Vector3.new(0, 500000, 0)
bodyG.P = 10000
bodyG.CFrame = CFrame.new(RootPart.Position, Mouse.Position)
bodyG.Parent = RootPart
Debris:AddItem(bodyG, 1)
end
local function MoveTowardsMouse(Mouse, Main)
local bodyV = Instance.new("BodyVelocity")
bodyV.MaxForce = Vector3.new(500000, 500000, 500000)
bodyV.Velocity = CFrame.new(Main.Position, Mouse.Position).LookVector * 100
bodyV.Parent = Main
local bodyG = Instance.new("BodyGyro")
bodyG.MaxTorque = Vector3.new(500000, 500000, 500000)
bodyG.P = 10000
bodyG.CFrame = CFrame.new(Main.Position, Mouse.Position)
bodyG.Parent = Main
end
FireRemote.OnServerEvent:Connect(function(Player, Mouse_CFrame)
if UhTable[Player.Name] == true then
return
end
UhTable[Player.Name] = true
local Character = Player.Character
local RootPart = Character:WaitForChild("HumanoidRootPart")
local folder = workspace:FindFirstChild("DebrisFolder") or Instance.new("Folder",workspace)
folder.Name = "DebrisFolder"
local RightHand = Character:WaitForChild("RightHand")
local FireSpear = ssFireSpear:Clone()
local Handle = FireSpear:WaitForChild("Handle")
local Hitbox = FireSpear:WaitForChild("Hitbox")
local Mesh = FireSpear:WaitForChild("Mesh")
FireSpear:SetPrimaryPartCFrame(RightHand.CFrame)
FireSpear.Parent = folder
local weld = Instance.new("Motor6D")
weld.Parent = Handle
weld.Part0 = RightHand
weld.Part1 = Handle
Hitbox:SetNetworkOwner(nil)
local function MakeStuffHappen()
spawn(function()
LookatMouse(Mouse_CFrame,RootPart)
wait(.6)
weld:Destroy()
MoveTowardsMouse(Mouse_CFrame,Hitbox)
end)
end
MakeStuffHappen()
end)
I'm following a tutorial but I don't know how the issue got there.

Your error is pointing to the fact that you are trying to reference fields on an object that doesn't exist. In this case, it's the ´Mouse´ object, which you never supplied from the client.
To fix this, pass the mouse information in when you call the RemoteEvent's FireServer() function.
UIS.InputBegan:Connect(function(input, isTyping)
if isTyping then
return
elseif input.KeyCode == Enum.KeyCode.E and debounce and Character then
debounce = false
local mouseCFrame = Mouse.Hit
FireSpear:FireServer(mouseCFrame)
wait(cd)
debounce = true
end
end)

Related

Animation won't play

I've made a GUI that has a WorldModel and when a player joins their character shows up on that GUI. All this works well but when I try to add an idle animation to it, it just won't run. I've tried different ways, loading the animation with animator, humanoid, but it just doesn't work. No errors.
Server Script (Creating animator):
local play = game.StarterGui:WaitForChild("Menu").Play
local uis = game:GetService("UserInputService")
local remote = game:GetService("ReplicatedStorage"):WaitForChild("Events").AnimPlay
game.Players.PlayerAdded:Connect(function(player)
local gui = player.PlayerGui:WaitForChild("CharacterShow")
local viewportframe = gui:WaitForChild("ViewportFrame")
local ClonedChar = game.Players:CreateHumanoidModelFromUserId(player.UserId)
ClonedChar.Parent = game:GetService("ReplicatedStorage")
local animator = Instance.new("Animator")
animator.Parent = ClonedChar.Humanoid
remote:FireClient(player,ClonedChar)
play.MouseButton1Click:Connect(function()
gui:Destroy()
end)
end)
Local Script (Creating animation, animationtrack, playing it, also making the GUI work, it has a turning feature too):
local player = game.Players.LocalPlayer
local remote = game:GetService("ReplicatedStorage"):WaitForChild("Events").AnimPlay
local uis = game:GetService("UserInputService")
local currentX
local MouseInDisplay, HoldInDisplay = false, false
local play = player.PlayerGui:WaitForChild("Menu").Play
local gui = player.PlayerGui:WaitForChild("CharacterShow")
remote.OnClientEvent:Connect(function(ClonedChar)
print("received")
local viewportframe = gui:WaitForChild("ViewportFrame")
local VPFcam = Instance.new("Camera")
VPFcam.Parent = viewportframe
VPFcam.CFrame = CFrame.new(0,0,0)
viewportframe.CurrentCamera = VPFcam
ClonedChar.Parent = viewportframe.WorldModel
ClonedChar.Name = "Character"
ClonedChar.Humanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None
ClonedChar:SetPrimaryPartCFrame(CFrame.new(Vector3.new(0,0,-9.5),Vector3.new(0,0,0)))
local animator = ClonedChar.Humanoid.Animator
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://11282795546"
local animtrack = animator:LoadAnimation(animation)
animtrack.Looped = true
animtrack.Priority = Enum.AnimationPriority.Action
animtrack:Play()
uis.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
if MouseInDisplay == true then
HoldInDisplay = true
currentX = nil
end
end
end)
uis.InputEnded:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
HoldInDisplay = false
end
end)
viewportframe.MouseMoved:Connect(function(X,Y)
if HoldInDisplay == false then return end
if currentX then ClonedChar.PrimaryPart.CFrame *= CFrame.fromEulerAnglesXYZ(0,((X-currentX)*.025),0) end
currentX = X
end)
viewportframe.MouseEnter:Connect(function() MouseInDisplay = true end)
viewportframe.MouseLeave:Connect(function() MouseInDisplay = false end)
end)
The animation ID is valid, and the game's owner created it.

Debounce is not a valid member of Folder

Alright so I got this script from a tutorial and this is what I typed for the script Remotes
local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteData = game:GetService("ServerStorage"):WaitForChild("RemoteData")
local cooldown = 1
replicatedStorage.Remotes.Lift.OnServerEvent:Connect(function(player)
print("event launched")
if not remoteData:FindFirstChild(player.Name) then return "NoFolder" end
local debounce = remoteData[player.Name].Debounce
if not debounce then
debounce.Value = true
player.leaderstats.Power.Value = player.leaderstats.Power.Value + 5 * (player.leaderstats.Prestiges.Value + 1)
wait(cooldown)
debounce.Value = false
end
end)
but I have seem to get the error which is
Debounce is not a valid member of Folder "ServerStorage.RemoteData.OmegaHero2010"
here are some other scripts
Stats
local serverStorage = game:GetService("ServerStorage")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local power = Instance.new("NumberValue")
power.Name = "Power"
power.Parent = leaderstats
local prestige = Instance.new("NumberValue")
prestige.Name = "Prestiges"
prestige.Parent = leaderstats
local dataFolder = Instance.new("Folder")
dataFolder.Name = player.Name
dataFolder.Parent = serverStorage.RemoteData
local debounce = Instance.new("BoolValue")
debounce.Parent = dataFolder
end)
Module
local module = {}
local replicatedStorage = game:GetService("ReplicatedStorage")
function module.Lift()
replicatedStorage.Remotes.Lift:FireServer()
end
return module
Local Script
local module = require(script.Parent:WaitForChild("ModuleScript"))
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
script.Parent.Activated:Connect(function()
module.Lift()
end)
what is the problem here?
The line local debounce = remoteData[player.Name].Debounce is failing to find the child named "Debounce".
So, looking at how you created the BoolValue in ServerScriptService :
local debounce = Instance.new("BoolValue")
debounce.Parent = dataFolder
You never set the Name property. So there is a BoolValue in the player's folder but it is named "BoolValue", not "Debounce". To fix your error, just add the line to set the Name.
debounce.Name = "Debounce"

How to get local player

I'm making a transformation script and I'm trying to get the LocalPlayer but this error appears:
13:54:03.806 ServerScriptService.SSJScript:2: attempt to index nil with 'GetMouse'  -  Server  -  SSJScript:2
How to fix?
This is my code:
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local chr = plr.Character
local offence = {
"AAARGH!! I HATE YOU!!",
"FUS RO DAH!",
"I'M COMING FOR THE SAAUCE!",
"IT IS TIME TO PERISH!",
"POOTIS POOTIS POOTIS POOTIS!"
}
local transformcooldown = 10
local healthboost = 1000
local speedboost = 32
local aura = game.ReplicatedStorage.Aura
local hair = game.ReplicatedStorage.Hair
local anim = chr.Humanoid:LoadAnimation(game.ReplicatedStorage.Animation)
local iscooldown = false
local ison = plr.SSJOnOff.Value
local cooldown = 10
game.Players.PlayerAdded:Connect(function(plrjoin)
plr = plrjoin
end)
mouse.KeyDown:connect(function(key)
if iscooldown == false then
key = string.lower(key)
if string.byte(key) == 48 then
local indicator = plr.SSJOnOff
if indicator.Value == false then
chr.Humanoid.WalkSpeed = 0
ison = true
game:GetService("Chat"):Chat(plr.Head,offence[math.random(1,#offence)])
wait(1)
anim:Play()
wait(1.4)
aura:Clone().Parent = chr.HumanoidRootPart
hair:Clone().Parent = chr
wait(0.6)
chr.Humanoid.MaxHealth = healthboost
chr.Humanoid.Health = healthboost / 2
chr.Humanoid.WalkSpeed = chr.Humanoid.WalkSpeed + speedboost
else
ison = false
iscooldown = true
chr.Hair:destroy()
chr.HumanoidRootPart.Aura:destroy()
chr.Humanoid.WalkSpeed = chr.Humanoid.WalkSpeed - speedboost
chr.Humanoid.Health = 100
chr.Humanoid.MaxHealth = 100
wait(cooldown)
end
end
end
The code need to be in a LocalScript part.
The function GetMouse() is on the screen on the Player so the Server cant know where is your mouse.
PS: game.Players.LocalPlayer is nil because it's a ServerScript and not a LocalScript
Write this code in a localscript and NOT in ServerScriptService
local player = game.Players.LocalPlayer

Roblox Lua cannot access modulescript function

I've been trying to connect a ModuleScript to a server Script, and it's throwing the error: Workspace.Script:6: attempt to call field 'check3' (a nil value)
ModuleScript
local GunStats = {}
local part = workspace:WaitForChild('Mosin'):WaitForChild("Union")
local billboard = workspace:WaitForChild('BillboardPart'):WaitForChild('BillboardGui')
local uis = game:GetService("UserInputService")
local Ekey = Enum.KeyCode.E
local check = false
local start = tick()
local function onpress(action1)
if check then
part.BrickColor = BrickColor.new("Black")
end
end
local function isKeydown(startTime)
return uis:IsKeyDown(startTime) and startTime - tick() <= 5
end
local function Input(input, gameprocessed)
if isKeydown(Ekey) then
print("h")
else
print("n")
end
start = tick()
end
game.Players.PlayerAdded:Connect(function(player)
local character = player.Character or player.CharacterAdded:Wait()
local humRoot = character:WaitForChild("HumanoidRootPart")
uis.InputBegan:Connect(Input)
GunStats.check3 = function()
while wait() do
if (humRoot.Position - part.Position).Magnitude < 5 then
print("IN RANGE")
check = true
billboard.Enabled = true
repeat wait() until (humRoot.Position - part.Position).Magnitude >= 5
elseif (humRoot.Position - part.Position).Magnitude > 5 and check then
check = false
print("OUT OF")
billboard.Enabled = false
end
end
end
end)
return GunStats
Server Script:
local players = game:GetService("Players")
local serverStorage = game.ServerStorage
local gunStats = require(serverStorage:WaitForChild("ModuleScript"))
game.Players.PlayerAdded:Connect(function(players)
gunStats.check3(players)
end)
This issue is because the gunStats.check3() function isn't defined on the GunStats object until after a player joins. I would restructure your ModuleScript so that GunStats.check3() is defined right away :
--[[ define all your helper functions up here ... ]]
local GunStats = {}
function GunStats.check3(player)
-- access the humanoid
local character = player.Character or player.CharacterAdded:Wait()
local humRoot = character:WaitForChild("HumanoidRootPart")
-- do the checks
end
return GunStats

script only working in Studio

//EDIT: yes i added the 2nd parameter GPE and if not gpe then return end in case someone nitpicks unnecessary details
Another problem is that it is quite long but if you read it and see something wrong then let me know. Also I have put this as a LocalScript in Startpack.
local uis = game:GetService("UserInputService")
local torso = script.Parent.Parent.Character.Torso or script.Parent.Parent.Character.UpperTorso
local TweenService = game:GetService("TweenService")
local db = true
function move(x)
local velocity = Instance.new("BodyVelocity")
velocity.Velocity = torso.CFrame.lookVector*50
velocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
velocity.Parent = x
end
uis.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.R and db then
db = false
local part = game:GetService("ReplicatedStorage").Union:Clone()
part.CFrame = torso.CFrame + torso.CFrame.lookVector*10
part.Parent = game.Workspace
local effect = game.ReplicatedStorage.Waterfall:Clone()
effect.CFrame = part.CFrame
local light = game:GetService("ReplicatedStorage").PointLight:Clone()
light.Parent = part
local weld = Instance.new("ManualWeld")
weld.Part0 = part
weld.Part1 = effect
weld.Parent = part
effect.Parent = part
local goal3 = {}
goal3.Size = Vector3.new(120,70,70)
local goal4 = {}
goal4.Size = goal3.Size
local tweenInfo = TweenInfo.new(2)
local tween3 = TweenService:Create(part, tweenInfo, goal3)
local tween4 = TweenService:Create(effect,tweenInfo, goal4)
tween3:Play(); tween4:Play()
game.Lighting.Blur.Enabled = true; game.Lighting.ColorCorrection.Enabled = true
move(part)
part.Anchored = false
effect.Anchored = false
move(effect)
local goal = {}
goal.Size = Vector3.new(30,7,7)
goal.Transparency = 0.4
local goal2 = {}
goal2.Size = goal.Size
local tweenInfo = TweenInfo.new(3)
local tween = TweenService:Create(part, tweenInfo, goal)
local tween2 = TweenService:Create(effect,tweenInfo, goal2)
tween:Play(); tween2:Play()
game:GetService("Debris"):AddItem(part,2)
game:GetService("Debris"):AddItem(effect,2)
wait(.5)
game.Lighting.Blur.Enabled = false; game.Lighting.ColorCorrection.Enabled = false
db = true
end
end)
The thing is pretty straightforward, It doesn't work when I publish the game and play it, but it only works in Roblox Studio.
It is not Filtering Enabled yet, and still doesn't work. I don't know exactly.
Fixed it lol i just moved the localscript from starterpack to startercharacter :D

Resources