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 field 'LocalPlayer' (a nil value). Help??

Asked by 4 years ago

I made a treadmill that works and I made a leaderboard

game.Players.PlayerAdded:Connect(function(plr)
    local folder = Instance.new("Folder",plr)
    folder.Name = "leaderstats"
    local speed = Instance.new("IntValue",folder)
    speed.Name = "Speed"
    speed.Value = 1
end)

Then I made this

local speed = game.Players.LocalPlayer.folder.speed
local conveyor = game.Workspace.Treadmill.Conveyor

conveyor.Touched:Connect(function(hit)
    if hit ~= nil then
        while true do
            if hit.Parent ~= nil then
                if hit.Parent.Character ~= nil then
                    if hit.Parent.Character.Health ~= 0 then
                        wait(2)
                        speed.Value = speed.Value + 2
                    end
                end
            end
        end
    end
end)

But it says attempt to index field 'LocalPlayer' (a nil value)

Please help?

0
is it a local script? prooheckcp 340 — 4y
0
Change to server script OnaKat 444 — 4y
0
Localplayer can only be accessed via the client. You could use remote events. H3kken -4 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

I'm not entirely sure, but this might be an easy fix if you make a line at the start of the second script (which is hopefully a local script) that waits for the local player's character to load.

repeat wait() until game.Players.LocalPlayer.Character

local speed = game.Players.LocalPlayer.folder.speed
local conveyor = game.Workspace.Treadmill.Conveyor

conveyor.Touched:Connect(function(hit)
    if hit ~= nil then
        while true do
            if hit.Parent ~= nil then
                if hit.Parent.Character ~= nil then
                    if hit.Parent.Character.Health ~= 0 then
                        wait(2)
                        speed.Value = speed.Value + 2
                    end
                end
            end
        end
    end
end)
0
that wouldn't change the fact that the server cant access the local player theking48989987 2147 — 4y
Ad

Answer this question