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

Attempt to index nil with 'WaitForChild' ?

Asked by 4 years ago

I am trying to make a thing that if when someone joins or dies, if they are a developer or have the VIP gamepass then it will give them double WalkSpeed and double JumpPower.

Here is my full normal script in workspace:

local ms = game:GetService("MarketplaceService")
local id = 9148194
game.Players.PlayerAdded:Connect(function(plr)
    local char = plr.Character
    local hum = char:WaitForChild("Humanoid")
    if ms:UserOwnsGamePassAsync(plr.UserId,id) then
        hum.WalkSpeed = 32
        hum.Died:Connect(function()
            wait(3)
            hum.WalkSpeed = 32
        end)
    elseif plr.UserId == 950965355 or plr.UserId == 714312166 then
        hum.WalkSpeed = 32
        hum.JumpPower = 100
        hum.Died:Connect(function()
            wait(3)
            hum.WalkSpeed = 32
            hum.JumpPower = 100
        end)
    end 
end) 

Please tell me what is wrong. Thanks in advance!

1 answer

Log in to vote
3
Answered by
Mayk728 855 Moderation Voter
4 years ago
Edited 4 years ago

it's because the character hasn't loaded yet when the script tried looking for it. also, you tried to set the walkspeed of the humanoid after the player has respawned, which will also cause error, because the old humanoid doesn't exist after respawning. i've added plr.CharacterAdded so that it fires each time the player respawns.

local ms = game:GetService("MarketplaceService")
local id = 9148194

game:GetService('Players').PlayerAdded:Connect(function(plr)
    repeat wait() until plr and plr.Character and plr.Character:FindFirstChild('Humanoid')
    local char,hum = plr.Character,plr.Character.Humanoid
    if ms:UserOwnsGamePassAsync(plr.UserId,id) then
        hum.WalkSpeed = 32
        plr.CharacterAdded:Connect(function(char)
            repeat wait() until char and char:FindFirstChild('Humanoid')
            local newhum = char.Humanoid
            newhum.WalkSpeed = 32
        end)
    elseif plr.UserId == 950965355 or plr.UserId == 714312166 then
        hum.WalkSpeed = 32
        hum.JumpPower = 100
        plr.CharacterAdded:Connect(function(char)
            repeat wait() until char and char:FindFirstChild('Humanoid')
            local newhum = char.Humanoid
            newhum.WalkSpeed = 32
            newhum.JumpPower = 100
        end)
    end 
end)
1
Thanks, I will try it out. PrismaticFruits 842 — 4y
Ad

Answer this question