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:

01local ms = game:GetService("MarketplaceService")
02local id = 9148194
03game.Players.PlayerAdded:Connect(function(plr)
04    local char = plr.Character
05    local hum = char:WaitForChild("Humanoid")
06    if ms:UserOwnsGamePassAsync(plr.UserId,id) then
07        hum.WalkSpeed = 32
08        hum.Died:Connect(function()
09            wait(3)
10            hum.WalkSpeed = 32
11        end)
12    elseif plr.UserId == 950965355 or plr.UserId == 714312166 then
13        hum.WalkSpeed = 32
14        hum.JumpPower = 100
15        hum.Died:Connect(function()
View all 21 lines...

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.

01local ms = game:GetService("MarketplaceService")
02local id = 9148194
03 
04game:GetService('Players').PlayerAdded:Connect(function(plr)
05    repeat wait() until plr and plr.Character and plr.Character:FindFirstChild('Humanoid')
06    local char,hum = plr.Character,plr.Character.Humanoid
07    if ms:UserOwnsGamePassAsync(plr.UserId,id) then
08        hum.WalkSpeed = 32
09        plr.CharacterAdded:Connect(function(char)
10            repeat wait() until char and char:FindFirstChild('Humanoid')
11            local newhum = char.Humanoid
12            newhum.WalkSpeed = 32
13        end)
14    elseif plr.UserId == 950965355 or plr.UserId == 714312166 then
15        hum.WalkSpeed = 32
View all 24 lines...
1
Thanks, I will try it out. PrismaticFruits 842 — 4y
Ad

Answer this question