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

Attempt to index field localPlayer error?

function onPlayerEntered(a)



	local b = Instance.new("IntValue")
	b.Name = "leaderstats"
	local c = Instance.new("IntValue")
	c.Name = "Points"
	c.Value = 0
	c.Parent = b
	b.Parent = a
end
game.Players.ChildAdded:connect(onPlayerEntered)
while wait(1) do
	game.Players.localPlayer.leaderstats["Points"].Value = game.Players.localPlayer.leaderstats["Points"].Value + 1
end

The script works in studio, but I get 'attempt to index field 'localPlayer' (a nil value) while actually playing the game and looking at the dev console.

3 answers

###This is because LocalPlayer is nil on the server. It can only be used in the client (Local Scripts). It is also called LocalPlayer with an uppercase L. localPlayer is deprecated and should not be used in new work.

local function onPlayerEnter(player) -- use local functions
    local ls = Instance.new("Configuration")
    ls.Name = "leaderstats"
    ls.Parent = player
    
    local points = Instance.new("IntValue")
    points.Name = "Points"
    points.Value = 0
    points.Parent = ls

    while true do
        points.Value = points.Value + 1
       wait(1)
    end
end

game:GetService("Players").PlayerAdded:Connect(onPlayerEnter) -- use PlayerAdded instead

###On a site note, switch to:Connect() as:connect() is deprecated and should not be used in new work. Also, use PlayerAdded instead of ChildAdded, as ChildAdded listens for any object type to be added, whilst PlayerAdded only listens for Player objects to be added.

LocalPlayer is a reference that can only be used in LocalScripts (hence the "local"). You also do not need to do all the excessive game.Players.LocalPlayer.leaderstats stuff. Since you already have the points value as a variable, you can just directly modify it from there.

function onPlayerEntered(player)
	local leaderstats = Instance.new("IntValue")
		leaderstats.Name = "leaderstats"

	local points = Instance.new("IntValue")
		points.Name = "Points"
		points.Value = 0
	
	points.Parent = leaderstats
	leaderstats.Parent = player

	while wait(1) do
		points.Value = points.Value + 1
	end
end

game.Players.PlayerAdded:Connect(onPlayerEntered)

6 comments

0

I have a problem. I have an ingame store, and when something is bought using these points the player's amount of points is approperiately subtracted by the cost of the item. However, about one second later the amount of points which the player has reverts back to the amount of points the player had before the charge. Also this is the script for the store.script.Parent.Text = tostring(script.Parent

0

Filtering is Enabled. Modifications through a local script (aka the client) will only appear to be modified until the server updates whatever you changed. Try using a remote

0

The thing is, the point value appears to be modified by the charge on the leaderboard, however it reverts back. It seems like it reverts back when a new point is added.

0

I tested it without FE and it appears to do the same thing.

0

Are you modifying the values in a local or server script?

0

Why you dont use a local like local Player = game:WaitForChild('Players').LocalPlayer

If you want help use this script:

function onPlayerEntered(plr)
     local b = Instance.new("Folder")
    b.Name = "leaderstats"
    local c = Instance.new("IntValue")
    c.Name = "Points"
    c.Value = 0
    c.Parent = b
    b.Parent = plr
end

game.Players.PlayerAdded:Connect(onPlayerEntered)

while wait(1) do
    game.Players.localPlayer.leaderstats["Points"].Value = game.Players.localPlayer.leaderstats["Points"].Value + 1
end

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