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 nil with 'WaitForChild' ?

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

✓ Accepted answer

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 comment

1

Thanks, I will try it out.

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