Attempt to index nil with 'leaderstats'

I want to make a obby game but when it finished the round it will give an points to the player’s team. But the problem is, it keeps saying attempt to index nil with 'leaderstats'

I’ve tried:

script:

function Player__Escaped(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
-- And few more script

if player.leaderstats.Player_Team.Value == 1 then
			game.Workspace.GameValueDetails.Team1Name.Score.Value = game.Workspace.GameValueDetails.Team1Name.Score.Value + AddToTeam
	end
-- few more script
end
script.Parent.EscapePath.Touched:Connect(Player__Escaped)

Error is in

if player.leaderstats.Player_Team.Value == 1 then

Other explanation that may be similar to answers:
No, I don’t wan’t to use Teams. I want to use value’s instead

Thank you so much for the help, I appreciate it! :smile:

function Player__Escaped(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	-- And few more script
	if not player then
		return
	end
	
	if player:WaitForChild("leaderstats"):WaitForChild("Player_Team").Value == 1 then
		game.Workspace.GameValueDetails.Team1Name.Score.Value = game.Workspace.GameValueDetails.Team1Name.Score.Value + AddToTeam
	end
	-- few more script
end

script.Parent.EscapePath.Touched:Connect(Player__Escaped)

Try this, I’ve added “WaitForChild()” commands to allow for the referenced instances to load before attempting to use them in the script, if you get an “infinite yield warning” in console from this then that means that “leaderstats” is not a valid child of the “Player” instance.

I’ve also forced a return statement if “player” is nil after an attempt has been made to assign to it a “Player” object.

1 Like

works at first but it doesn’t work in a second try, its supposed to give the team points and teleport player to lobby. but nothing happens.

If it works the first time but not on subsequent retries then there must be some other issue. Could you provide the full script?

local Place_Of_Enter = 1
function EscapeIsOpen()
	script.Parent.EscapePath.EscapeStatusGui.EscapePadIsLocked.Value = true
	script.Parent.ActivatePart.CanCollide = true
	script.Parent.ActivatePart.Transparency = 0
	script.Parent.ActivateButton.BrickColor = BrickColor.new("Really red")
end
function EscapePadIsOpen__Or__Close()
	if script.Parent.EscapePath.EscapeStatusGui.EscapePadIsLocked.Value then -- If its open then
		script.Parent.EscapePath.EscapeStatusGui.EscapeStatus.Text = "UNLOCKED"
		script.Parent.EscapePath.EscapeStatusGui.EscapeStatus.TextColor3 = Color3.fromRGB(98, 255, 74)
	else
		if not script.Parent.EscapePath.EscapeStatusGui.EscapePadIsLocked.Value then -- If its not open then
			script.Parent.EscapePath.EscapeStatusGui.EscapeStatus.Text = "LOCKED"
			script.Parent.EscapePath.EscapeStatusGui.EscapeStatus.TextColor3 = Color3.fromRGB(255, 16, 16)
		end
	end
end
function Player__Escaped(hit)
	if not db then
		db = true
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if not player then
			return
		end
		local AddToTeam = 0
		if Place_Of_Enter == 1 then
			AddToTeam = 1000
		else
			if Place_Of_Enter == 2 then
				AddToTeam = 750
			else
				if Place_Of_Enter == 3 then
					AddToTeam = 500
				else
					if Place_Of_Enter == 4 then
						AddToTeam = 250
					end
				end
			end
		end
		wait(0.5)
		print(Place_Of_Enter)
		Place_Of_Enter = Place_Of_Enter + 1
		wait(0.1)
		if player:WaitForChild("leaderstats"):WaitForChild("Player_Team").Value == 1 then
			game.Workspace.GameValueDetails.Team1Name.Score.Value = game.Workspace.GameValueDetails.Team1Name.Score.Value + AddToTeam
		end
		if player.leaderstats.Player_Team.Value == 2 then
			game.Workspace.GameValueDetails.Team1Name.Score.Value = game.Workspace.GameValueDetails.Team1Name.Score.Value + AddToTeam
		end
		if script.Parent.EscapePath.EscapeStatusGui.EscapePadIsLocked.Value then
			local character = hit.Parent
			if character:FindFirstChild("HumanoidRootPart") then
				character.HumanoidRootPart.Position = Vector3.new(-142, 8.25, 25.5)
				for i, v in pairs(game.Players:GetPlayers()) do
					local playerGui = v:WaitForChild("PlayerGui")
					playerGui.SpectatorVision.Playerhavescaped.Visible = true
					playerGui.SpectatorVision.Playerhavescaped.Text = character.Name.." has escaped the facility"
					wait(5)
					playerGui.SpectatorVision.Playerhavescaped.Visible = false
				end
			else
				character.Parent.HumanoidRootPart.Position = Vector3.new(-142, 8.25, 25.5)
			end
		end
		wait(1)
		db = false
	end
end
script.Parent.EscapePath.Touched:Connect(Player__Escaped)
script.Parent.EscapePath.EscapeStatusGui.EscapePadIsLocked.Changed:Connect(EscapePadIsOpen__Or__Close)
script.Parent.ActivateButton.Touched:Connect(EscapeIsOpen)```
function Player__Escaped(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	-- And few more script
	if not player then
		return
	end

	if player:WaitForChild("leaderstats"):WaitForChild("Player_Team").Value >= 1 then
		game.Workspace.GameValueDetails.Team1Name.Score.Value += AddToTeam
	end
	-- few more script
end

script.Parent.EscapePath.Touched:Connect(Player__Escaped)

Oh, I see your issue.

player:WaitForChild("leaderstats"):WaitForChild("Player_Team").Value == 1
Before you were only checking if that stat was 1 before increasing it.
I’ve changed this line of code to:
player:WaitForChild("leaderstats"):WaitForChild("Player_Team").Value >= 1
So now it’ll check if the value of the stat is equal to or greater than 1 and then change the stat.

no at first the stat was 0, but i change it by using Current: Server Button and make it 1 then go back to client. it started not working.
also i only put == not > because 1 and 2 will have difference on score. so if i put > then the team 2 wont have any score, it will placed on team. But i could try it again cause i might have a typo in the stats

Yeah, there’s likely some other issue with the script considering it works the first time but not for subsequent retries, do you get any errors in console when testing?

no, but i think i found the error, cause i have a hat accessory and when i jump it will touch the button which will teleport, and that maybe why the script may not work because of the hat. I’ll try it again without the hat touching it

Yep! it was an hat, thank you so much for the help, i will solution you as soon as possible, Thank you!

local Place_Of_Enter = 1
local db = false

function EscapeIsOpen()
	script.Parent.EscapePath.EscapeStatusGui.EscapePadIsLocked.Value = true
	script.Parent.ActivatePart.CanCollide = true
	script.Parent.ActivatePart.Transparency = 0
	script.Parent.ActivateButton.BrickColor = BrickColor.new("Really red")
end

function EscapePadIsOpen__Or__Close()
	if script.Parent.EscapePath.EscapeStatusGui.EscapePadIsLocked.Value then -- If its open then
		script.Parent.EscapePath.EscapeStatusGui.EscapeStatus.Text = "UNLOCKED"
		script.Parent.EscapePath.EscapeStatusGui.EscapeStatus.TextColor3 = Color3.fromRGB(98, 255, 74)
	elseif not script.Parent.EscapePath.EscapeStatusGui.EscapePadIsLocked.Value then -- If its not open then
		script.Parent.EscapePath.EscapeStatusGui.EscapeStatus.Text = "LOCKED"
		script.Parent.EscapePath.EscapeStatusGui.EscapeStatus.TextColor3 = Color3.fromRGB(255, 16, 16)
	end
end

function Player__Escaped(hit)
	if not db then
		db = true
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if not player then
			return
		end
		local AddToTeam = 0
		if Place_Of_Enter == 1 then
			AddToTeam = 1000
		elseif Place_Of_Enter == 2 then
			AddToTeam = 750
		elseif Place_Of_Enter == 3 then
			AddToTeam = 500
		elseif Place_Of_Enter == 4 then
			AddToTeam = 250
		end
		wait(0.5)
		print(Place_Of_Enter)
		Place_Of_Enter = Place_Of_Enter + 1
		wait(0.1)
		if player:WaitForChild("leaderstats"):WaitForChild("Player_Team").Value == 1 then
			game.Workspace.GameValueDetails.Team1Name.Score.Value = game.Workspace.GameValueDetails.Team1Name.Score.Value + AddToTeam
		end
		if player:WaitForChild("leaderstats"):WaitForChild("Player_Team").Value == 2 then
			game.Workspace.GameValueDetails.Team1Name.Score.Value = game.Workspace.GameValueDetails.Team1Name.Score.Value + AddToTeam
		end
		if script.Parent.EscapePath.EscapeStatusGui.EscapePadIsLocked.Value then
			local character = hit.Parent
			if character:FindFirstChild("HumanoidRootPart") then
				character.HumanoidRootPart.Position = Vector3.new(-142, 8.25, 25.5)
				for i, v in pairs(game.Players:GetPlayers()) do
					local playerGui = v:WaitForChild("PlayerGui")
					playerGui.SpectatorVision.Playerhavescaped.Visible = true
					playerGui.SpectatorVision.Playerhavescaped.Text = character.Name.." has escaped the facility"
					wait(5)
					playerGui.SpectatorVision.Playerhavescaped.Visible = false
				end
			else
				character.Parent.HumanoidRootPart.Position = Vector3.new(-142, 8.25, 25.5)
			end
		end
		wait(1)
		db = false
	end
end

script.Parent.EscapePath.Touched:Connect(Player__Escaped)
script.Parent.EscapePath.EscapeStatusGui.EscapePadIsLocked.Changed:Connect(EscapePadIsOpen__Or__Close)
script.Parent.ActivateButton.Touched:Connect(EscapeIsOpen)

You were referencing “db” in the script without declaring it. I’ve also fixed up some of the conditional chains you had from “else if” to just “elseif”.

1 Like

alright thanks! for the help. i appreciate it! :smile: