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

How to fix "Attempt index nil with 'leaderstatstwo' "?

I'm having a problem with one of my scripts. I have it coded to where every time you pick up this chicken it should give you a point every second. I have this set up and working for kills, however when I do it with the point system it gives me an error that says, "attempt to index nil with 'leaderstatstwo' ". I don't know what the problem is. This is the script inside the chicken, where the error is occuring.

while true do
			if script.Parent.Count.Value == true then
				local plr = game.Players:GetPlayerFromCharacter(script.Parent.Parent.Parent)
				wait(1)
				plr.leaderstatstwo.ChickenLB.Value = plr.leaderstatstwo.ChickenLB.Value + 1
			end
			wait()
		end

This is the script that spawns the folder inside the player.

game.Players.PlayerAdded:Connect(function(player)
	local foldertwo = Instance.new("Folder",player)
	foldertwo.Name = "leaderstatstwo"
	local chickenlb = Instance.new("IntValue",foldertwo)
	chickenlb.Name = "ChickenLB"
end)

Third script found inside startergui>ScreenGUI>TextLabel, this is the local script.

while wait() do
	local player = game.Players.LocalPlayer
	script.Parent.Text = "Chicken Points: "..player:WaitForChild("leaderstatstwo"):FindFirstChild("ChickenLB").Value
end

It even shows the folder and the IntValue whenever you play and look under the player's folder.

3 comments

0

EDIT: It spawns a folder inside the player. The script however is inside the workspace.

0

is the first code inside a normal or local script?

0

Normal script

3 answers

GetPlayerFromCharacter() will return nil if the Player is not found. To stop this you must add a check to see if it has not returned nil.

if not plr then return end

2 comments

0

I put the code you showed right under plr.leaderstatstwo.ChickenLB.Value = plr.leaderstatstwo.ChickenLB.Value. It then gives me the same error 5 times then stops and does nothing

0

you need to put it under where you first define player

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