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

Attemp to index nil with value. How do i fix this?

Asked by 4 years ago

the problem is on line 13

local plrs = game:GetService("Players")

local plr = plrs.LocalPlayer

for i,v in pairs (workspace.Spawns:GetChildren()) do
    if v:IsA("Part") then
        v.Touched:Connect(function(hit)
            if hit.Parent:FindFirstChild("Humanoid") then
                local player = game.Players:GetPlayerFromCharacter(hit.Parent)
                if tonumber (v.Name) > player.leaderstats.Stage.Value then
                    if player.leaderstats.Stage.Value ~= tonumber(v.Name) then
                        player.leaderstats.Stage.Value = tonumber(v.Name)
                        plr.Gems.Value = plr.Gems.Value + plr.Stage.Value
                    end
                end
            end
        end)
    end
end

local respawnDDelay = 0

game.Players.CharacterAutoLoads = false

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

    plr.CharacterAdded:Connect(function(char)
        local hum = char:FindFirstChild("Humanoid")
        if hum then
            hum.Died:Connect(function()
                wait(respawnDDelay)
                plr:LoadCharacter()
                plr.Character:FindFirstChild("HumanoidRootPart").CFrame = workspace.Spawns[plr.leaderstats.Stage.Value].CFrame
            end)
        end
    end)
    plr:LoadCharacter()
end)

1 answer

Log in to vote
0
Answered by
Zrxiis 26
4 years ago

In the beginning of your code you set the player "plr" to "plrs.LocalPlayer". I'm assuming this is in a server script and "local player" can't be used in a server script so it would be nil which is why using .Value on Gems would also be nil and create your error.

https://developer.roblox.com/en-us/api-reference/property/Players/LocalPlayer

It might be better to instead make it a function and pass the player to it from when the player is added, I could be wrong though on how to get the player.

local respawnDDelay = 0

game.Players.CharacterAutoLoads = false


game.Players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(char)
        local hum = char:FindFirstChild("Humanoid")
        if hum then
            hum.Died:Connect(function()
                wait(respawnDDelay)
                plr:LoadCharacter()
                plr.Character:FindFirstChild("HumanoidRootPart").CFrame = workspace.Spawns[plr.leaderstats.Stage.Value].CFrame
            end)
        end
    end)
    plr:LoadCharacter()

    for i,v in pairs (workspace.Spawns:GetChildren()) do
        if v:IsA("Part") then
            v.Touched:Connect(function(hit)
                if hit.Parent:FindFirstChild("Humanoid") then
                    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
                    if tonumber (v.Name) > player.leaderstats.Stage.Value then
                        if player.leaderstats.Stage.Value ~= tonumber(v.Name) then
                            player.leaderstats.Stage.Value = tonumber(v.Name)
                            plr.Gems.Value = plr.Gems.Value + plr.Stage.Value
                        end
                    end
                end
            end)
        end
    end
end)
Ad

Answer this question