Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

attempt to index local 'humanoid' (a nil value)???

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

2 answers

✓ Accepted answer

You don't have to use FindFirstChild() to get the Character and the Humanoid. You can use plr.Character and then char.Humanoid.

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 = plr.Character --Changed from getting the character from workspace from plr name
    local humanoid = char.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)
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:WaitForChild("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)

I recommend using waitforchild to prevent any errors.

1 comment

0

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

This discussion is preserved as it was. New answers, comments, and votes are closed.