Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

I keep getting this error, attempt to index field 'LocalPlayer' (a nil value). Help??

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?

3 comments

0

is it a local script?

0

Change to server script

0

Localplayer can only be accessed via the client. You could use remote events.

1 answer

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)

1 comment

0

that wouldn't change the fact that the server cant access the local player

This discussion is preserved as it was. New answers, comments, and votes are closed.