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

'attempt to index a nil value'?

Asked by
Yurash 0
6 years ago
Edited 6 years ago

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)

0
Mind if you share the leaderstats Script? Cause I've tested it any I didn't get an error :/ MRbraveDragon 374 — 6y
0
Sure! I added it to the main post @MRbraveDragon. ^_^ Yurash 0 — 6y
0
Does the error occur when you touch a checkpoint, when you start the game, or just happens randomly? Because I don't really see why you would get an error. MRbraveDragon 374 — 6y
0
It happens when I start the game, but as I said in the main post it doesn't happen ALL the time... sometimes it works but then when it's stopped working it stays like that for ages until it for some reason starts working again... :/ At the moment my published game (it's private at the moment) works, but the saved Studio file doesn't... Yurash 0 — 6y
View all comments (2 more)
0
However, if I publish the game the error gets published too. Maybe the other error I have is the cause? I'm also getting a 'startScript re-entrancy has exceeded 3' error when I start it, though that also happens when the Obby loads with the stages. The startScript error doesn't seem to tell me where the error is and just takes me to the 'Anti-Lag' file, line 33 'l.Parent = rando(game.Workspace)' Yurash 0 — 6y
0
Sorry for the triple post, but I just went in to test it again and the error DOES happen when the checkpoint brick is touched... (I didn't notice before as the spawn was right on top of the first checkpoint and when you spawn in you land on the Stage 1 brick). Maybe that information helps, I don't know haha XD Yurash 0 — 6y

Answer this question