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

attempt to index nil with 'FindFirstChild'?

Hey so im trying to set Stage (S) as a variable and for some reason it just doesnt work. i did the same line for so many scripts and it just doesnt work for me.

here's the code:

`local ChangeOEvent = script.Parent.Parent.ReplicatedStorage.ChangeObbySpawn

ChangeOEvent.OnServerEvent:Connect(function(Player)
local players = game:GetService("Players")
local Cpoints = game.Workspace.CheckPoints:GetChildren()
local obbyv =  game.StarterGui.InsertedObjects.ObbyButton.Value
local tycoonv =  game.StarterGui.InsertedObjects.TycoonButton.Value
local Teams = game:GetService("Teams")
local Cpoints = game.Workspace.CheckPoints	

local leaderstats = players:FindFirstChild("leaderstats")
local S = leaderstats:FindFirstChild("Stage")
if Player.Team == Teams.Red then
	obbyv.Value = true
	tycoonv.Value = false
	if obbyv.Value == true then

	

		local hum
		Player.PlayerGui.InsertedObjects.ObbyButton.BackgroundColor3 = Color3.new(0.545098, 0.545098, 0)
		Player.PlayerGui.InsertedObjects.ObbyButton.Shadow.BackgroundColor3 = Color3.new(0.282353, 0.282353, 0)

		Player.PlayerGui.InsertedObjects.TycoonButton.BackgroundColor3 = Color3.new(255, 0, 0)
		Player.PlayerGui.InsertedObjects.TycoonButton.Shadow.BackgroundColor3 = Color3.new(0.290196, 0, 0)
		local char = Player.Character
		if char then
			hum = char:FindFirstChild("Humanoid")
			Player.CharacterAdded:Connect(function(hum)
				hum:MoveTo(Cpoints[S.Value].Position)
			end)
		end
	end
end

end) `

1 answer

The reason why this is not working is because you were accessing leaderstats from game.Players, rather than the actual player itself. When it says "attempt to index nil", it's basically saying it's trying to access something that doesn't exist, which in this case is leaderstats. Your leaderstats existed in the player, not the game.Players.

The fix for this code would be relatively simple. Just change where you access leaderstats.

ChangeOEvent.OnServerEvent:Connect(function(Player)
local Cpoints = game.Workspace.CheckPoints:GetChildren()
local obbyv =  game.StarterGui.InsertedObjects.ObbyButton.Value
local tycoonv =  game.StarterGui.InsertedObjects.TycoonButton.Value
local Teams = game:GetService("Teams")
local Cpoints = game.Workspace.CheckPoints  

local leaderstats = Player:FindFirstChild("leaderstats")
local S = leaderstats:FindFirstChild("Stage")
if Player.Team == Teams.Red then
    obbyv.Value = true
    tycoonv.Value = false
    if obbyv.Value == true then



        local hum
        Player.PlayerGui.InsertedObjects.ObbyButton.BackgroundColor3 = Color3.new(0.545098, 0.545098, 0)
        Player.PlayerGui.InsertedObjects.ObbyButton.Shadow.BackgroundColor3 = Color3.new(0.282353, 0.282353, 0)

        Player.PlayerGui.InsertedObjects.TycoonButton.BackgroundColor3 = Color3.new(255, 0, 0)
        Player.PlayerGui.InsertedObjects.TycoonButton.Shadow.BackgroundColor3 = Color3.new(0.290196, 0, 0)
        local char = Player.Character
        if char then
            hum = char:FindFirstChild("Humanoid")
            Player.CharacterAdded:Connect(function(hum)
                hum:MoveTo(Cpoints[S.Value].Position)
            end)
        end
    end
end

I removed the players line, because it's unneccessary, since you're not using the player list, you're getting the player straight up from who fired the remote event.

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