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

I keep getting this error attempt to index local 'level' (a nil value). Help?

Asked by 5 years ago
Edited 5 years ago
game.Players.PlayerAdded:Connect(function(plr)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = plr
    local stage = Instance.new("IntValue",leaderstats)
    stage.Parent = leaderstats
    stage.Value = 1
    stage.Name = "Stage"
    plr.CharacterAdded:Connect(function(Character)
        repeat wait() until Character ~= nil
        local Level = game.Workspace.AllCheckpoints:FindFirstChild(stage.Value)
        Level.Value = stage.Value
        Character:WaitForChild("HumanoidRootPart").CFrame = Level.stage + Vector3(0,5,0)
    end)
end)

I dont know what is wrong with this script. Could you help me. It says "attempt to index local 'level' (a nil value)" Please help me It says there is an error in line 12.

0
Your code is so messy and hard to read.. lol H3kken -4 — 5y
0
I edited it, you should see it correctly now TomasGoldFinch 7 — 5y

2 answers

Log in to vote
2
Answered by 5 years ago

FindFirstChild will return the child instance with the given name of nil if no child with that given name is found.

In your code you are passing a int (number) as a parameter to FindFirstChild which expects a string. I would just use a string for simplicity at this stage. Secondly you would need a fall back position if something goes wrong this would be the default spawn position.

e.g.

if Level ~= nil then
    -- spawn to level
else
    -- spawn to default
end

Other points

repeat wait() until Character ~= nil would never be nil and can cause issues as you are using wait(). I would remove this.

The parent argument for Instance.new should not be used. You sould setup the instance and then set its parent last. The root instance (what you parent to the game / workspace) should be parented last.

in short follow create, setup, parent to instance, parent to game. This will reduce overheads with replicating changes in the game ie only do it once.

Putting this all together.

game.Players.PlayerAdded:Connect(function(plr)

    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"

    local stage = Instance.new("StringValue")
    stage.Value = "1"
    stage.Name = "Stage"
    stage.Parent = leaderstats

    leaderstats.Parent = plr -- parent to game last

    plr.CharacterAdded:Connect(function(Character)
        local Level = game.Workspace.AllCheckpoints:FindFirstChild(stage.Value)

        if Level then
            --Level.Value = stage.Value -- i am not sure why you want to do this it would break for everyone
            Character:WaitForChild("HumanoidRootPart").CFrame = Level.stage + Vector3(0,5,0)
        else
            -- default ??
        end
    end)
end)

I hope this helps.

Ad
Log in to vote
0
Answered by 5 years ago

You didn't capitalize level, even though you did as a variable.

game.Players.PlayerAdded:Connect(function(plr) 
    local leaderstats = Instance.new("Folder") 
    leaderstats.Name = "leaderstats" 
    leaderstats.Parent = plr 
    local stage = Instance.new("IntValue",leaderstats) 
    stage.Parent = leaderstats 
    stage.Value = 1 
    stage.Name = "Stage" 
    plr.CharacterAdded:Connect(function(Character) 
        repeat wait() until Character ~= nil 
        local Level = game.Workspace.AllCheckpoints:FindFirstChild(stage.Value) 
        Level.Value = stage.Value 
        Character:WaitForChild("HumanoidRootPart").CFrame = Level.stage + Vector3(0,5,0) -- You capitalized level as a variable but you forgot to capitalize it when you said "level.stage"
    end) 
end)
0
It still says the same error TomasGoldFinch 7 — 5y

Answer this question