im having a problem with a punching script where when the character presses the key to punch it throws me a error saying " ServerScriptService.Punch:16: attempt to index local 'humanoid' (a nil value)"
heres the script
math.randomseed(tick())
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local punchEvent = Instance.new("RemoteEvent", ReplicatedStorage)
punchEvent.Name = "PunchEvent"
local animations = {3131953009, 3131953009}
local function onPunchFired(plr)
local char = game.Workspace:FindFirstChild(plr.Name)
local humanoid = char:FindFirstChild("Humanoid")
local animation = Instance.new("Animation")
local picked = math.random(1, #animations)
animation.AnimationId = "http://roblox.com/asset/?id="..animations[picked]
local animTrack = humanoid:LoadAnimation(animation)
animTrack:Play()
local dmgScript = script.DmgScript:Clone()
if picked == 1 then
dmgScript.Parent = char.RightHand
elseif picked == 2 then
dmgScript.Parent = char.LeftHand
end
dmgScript.Disabled = false
wait(0.4)
dmgScript:Destroy()
animTrack:Stop()
end
punchEvent.OnServerEvent:Connect(onPunchFired)
it says the error is at line 15,16

1 comment
You shouldn't have to use WaitForChild(), the player and it's character should already be loaded in by the time this is called.