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

Attempt to index field localPlayer error?

Asked by 6 years ago
Edited 6 years ago
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

Log in to vote
0
Answered by 6 years ago

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.

Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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)
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 Aelerity 5 — 6y
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 YTRaulByte 389 — 6y
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. Aelerity 5 — 6y
0
I tested it without FE and it appears to do the same thing. Aelerity 5 — 6y
View all comments (2 more)
0
Are you modifying the values in a local or server script? YTRaulByte 389 — 6y
0
Why you dont use a local like local Player = game:WaitForChild('Players').LocalPlayer DodaKiller_YT 2 — 3y
Log in to vote
-1
Answered by 6 years ago

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

Answer this question