Hello! I'm building an Obby right now and it took me a long time to find a working stage and leaderboard script. I managed to find one but it seems to only work some of the time*... I keep getting two errors, but in this question I will only ask for help on one... might as well focus on them one at a time!
*as in it will sometimes work perfectly for all tests and then just seem to break at a random moment...
The full error is:
17:18:23.779 - Workspace.Checkpoints.ChangeUserStageOnTouch:10: attempt to index a nil value
The file throwing the error is a normal script file:
local x=script.Parent:GetChildren() for i=1,#x do if x[i]:IsA("BasePart") then if tonumber(x[i].Name) then local Num=tonumber(x[i].Name) x[i].Touched:connect(function(hit) local player = game.Players:GetPlayerFromCharacter(hit.Parent) if player then if player:WaitForChild("leaderstats",1):WaitForChild("Stage",1).Value == (Num - 1) then if hit.Parent.Humanoid.Health ~= 0 then player.leaderstats.Stage.Value = Num end end end end) end end end
I know line 10 is the issue but as I haven't scripted using Roblox before I'm not sure how to fix it...
I read something on here regarding a similar problem but I couldn't see how I would solve this for myself... thanks for any help! I feel I will be asking a few questions here haha.
EDIT: Added the leaderboard script -
DS1=game:GetService("DataStoreService"):GetDataStore("STAGE_1") game.Players.PlayerRemoving:connect(function(Player) DS1:SetAsync(Player.UserId,Player.leaderstats.Stage.Value) end) game.Players.PlayerAdded:connect(function(Player) local Folder = Instance.new("Folder") Folder.Name = "leaderstats" local Stage = Instance.new("IntValue") Stage.Name = "Stage" if DS1:GetAsync(Player.UserId) then Stage.Value = DS1:GetAsync(Player.UserId) else Stage.Value = 1 end Stage.Parent = Folder Folder.Parent = Player Player.CharacterAdded:Connect(function() -- The function object or character property of the player DON'T WORK for line 29! WTF! So line 25 + line 29 work fine. local Character=workspace:WaitForChild(Player.Name,5) local x=workspace:WaitForChild("Checkpoints",5):FindFirstChild(Player:WaitForChild("leaderstats",5):WaitForChild("Stage",5).Value) if x then if game.Players:GetPlayerFromCharacter(Character) then Character:MoveTo(x.Position) end print("Found matching stage: ".. tostring(x) ..". Moving [".. string.upper(Character.Name) .."] to (".. tostring(x.Position) ..").") end end) Player:LoadCharacter() end)