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.
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
: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)
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