Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

attempt to index nil with 'FindFirstChild'?

Asked by 2 years ago

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

Log in to vote
0
Answered by 2 years ago

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.

Ad

Answer this question