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

Why does it keep saying that my value is nil? how can i fix that?

local players = game:GetService("Players")

local Humanoid = script.Parent.Humanoid
function Died()
	wait(0.2)
	local tag = Humanoid:WaitForChild("creator")
	if tag ~= nil and tag.Value ~= nil then 
		local plr = players:GetChildren(tag.Value)
		if plr ~= nil then
			local leaderstats = plr:GetChildren("leadertats")
			local cash = plr.leaderstats.Cash
			local kills = plr.leaderstats.Kills
			cash.Value = cash.Value + 0
			kills.Value = kills.Value + 1 
			wait(0.1) 
			script:remove() 
		end 
	end 
end 
Humanoid.Died:connect(Died)

ERROR : 10: attempt to call a nil value

Thanks for the help! :)

3 answers

✓ Accepted answer

'GetChildren' doesn't accept any values. Consider using 'FindFirstChild' for it.

First off, GetChildren returns a table of all the children of the object, it won't find your value. Best thing for this would to be to use FindFirstChild or Waitforchild and then define which child you want to get. Also, I'm guessing you want to get the player not the service 'Players'. If you want to access the player through a script the you should use the PlayerAdded event and save the player who joined to a variable (in the first parameter). If it's a local script the you should use game.Players.LocalPlayer. Also Remove is deprecated, use destroy instead. Sorry if its unclear I'm on mobile.

Hope that helped!

JeffTheEpicRobloxian

:GetChildren() as stated above by Jeff returns a table of all the children inside of what you chose, if you wanted to get everything inside the character named 'leaderstats' (presuming you have more than 1), you could create a variable for the children of the character, then loop through it and then do whatever to each individual one.

local children = plr:GetChildren()
for _, child in pairs(children) do
	if child.Name == "leaderstats" then
		--do stuff
	end
end

But I'm 99% sure there's only going to be one individual folder named leaderstats and it was just some misuse of getchildren, try using:

local leaderstats = plr.leaderstats

OR

local leaderstats = plr:FindFirstChild("leaderstats")

OR

local leaderstats = plr:WaitForChild("leaderstats")

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