function setupStats(plr)
local stats = Instance.new(“Folder”)
stats.Name = “leaderstats”
local stage = Instance.new(“NumberValue”)
stage.Name = “Stage”
stage.Value = 1
stats.Parent = plr
stage.Parent = stats
end
function AdvanceStage(plr,checkpoint)
local p = game.Players:GetPlayerFromCharacter(plr)
local stage = p:FindFirstChild(‘leaderstats’):FindFirstChild(‘Stage’)
if p and stage~=nil and plr:FindFirstChild(‘Humanoid’).Health>0 then
if(stage.Value+1) == tonumber(checkpoint) then
stage.Value = stage.Value+1
end
else
return
end
end
game.Players.PlayerAdded:connect(function(p)
setupStats(p)
p.CharacterAdded:connect(function(c)
local curStage = p:WaitForChild(‘leaderstats’):WaitForChild(‘Stage’)
if curStage~=nil and checkpointDir:WaitForChild(tostring(curStage.Value)) then
local point = checkpointDir:WaitForChild(tostring(curStage.Value))
wait(.3)
c:WaitForChild(‘HumanoidRootPart’).CFrame = CFrame.new(point.Position+Vector3.new(0,5,0))
end
end)
end)
for i,v in pairs(checkpointDir:GetChildren()) do
v.Touched:connect(function(h)
if h.Parent:WaitForChild(‘Humanoid’) then
AdvanceStage(h.Parent,v.Name)
end
end)
end
Or a print(p) … look over your structure in the explorer window … is everything where it should be?
Is everything spelled the same as the script … things like that.
this part: game.Players.PlayerAdded:connect(function(p)
looks like a server script so: stats.Parent = plr would not go to the right place …
“Attempt to index nil with FindFirstChild” basically means nil:FindFirstChild() which is not desired.
As @NyrionDev stated, you can also do this instead:
local p = game.Players:GetPlayerFromCharacter(plr)
if not p then
print("Player is nil!")
return
else
local stage = ...
end
When encountering occasions such as those where a variable could be nil, instead of directly indexing it, make sure it exists first!
Also, did you pass a Model to the function? That model should be a player character. If not, what part are you passing to the function? In case of it being a limb, you may index its parent.
The :GetPlayerFromCharacter() requieres a character not a player as an argument
so change the code to this :
--Function AdvanceToNextStage
local character = plr.Character or plr.CharacterAdded:Wait()
local p = game.Player:GetPlayerFromCharacter(character)
--Do The Rest
This error message literally means the variable you’re trying to index is nil. Backtrack your script and see what’s going wrong. If it’s meant to sometimes be nil, then return the script if so, or make a pcall() wraparound.