Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 5 years ago

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

Log in to vote
0
Answered by 5 years ago

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)
Ad
Log in to vote
0
Answered by 5 years ago
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.

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. BennyBoiOriginal 293 — 5y

Answer this question