Still have questions? Join our Discord server and get real time help.
Log in to vote

Error: Player attempt to index field LocalPlayer (A Nil Value)?

Asked by 10 days ago

local a ="Folder",plr)

a.Name = "leaderstats"

local m ="IntValue",a)

m.Value = 0

m.Name = "Cash"

local x ="IntValue",a)

x.Value = 0

x.Name = "Bananas"


while true do


for i, v in pairs(game.Players:GetChildren()) do

if v:FindFirstChild("leaderstats") ~= nil then

if v.leaderstats:FindFirstChild("Bananas") ~= nil then

if game.Players.LocalPlayer.leaderstats.Cash.Value == 0


v.leaderstats.Bananas.Value = v.leaderstats.Bananas.Value else

v.leaderstats.Bananas.Value = v.leaderstats.Bananas.Value +10 end




What line is it on? MCAndRobloxUnited 1517 — 10d
line 35 Narrowlegobricks 6 — 10d
c o d e b l o c k s ! ! ! ! LoganboyInCO 125 — 10d

1 answer

Log in to vote
Answered by 10 days ago
Edited 10 days ago

Network Filtering

Network filtering, more commonly know as FilteringEnabled, or FE is the main cause of your problem here. As the server does not have a local player.

You can think of asking the server to get the local player as:

  • asking a parent to get the local child
  • asking a teacher to get the local student

Safe to say, it just creates a whole lot of confusion.

# What you can do

what you can do to avoid this is by using the players table you already have, instead of use the local player, like this:

lua for _,plr in pairs(game.Players:GetPlayers()) do local ls = plr:FindFirstChild("leaderstats") if ls then if not (ls.Cash.Value == 0) then ls.Bananas.Value = ls.Bananas.Value + 10 end end end

# a couple of extra things

  • do not use the 2nd parameter of, as it will have a negative impact on performance, due to the way roblox handles property listeners
  • Use GetPlayers() instead of GetChildren() to avoid getting non-player objects
  • For lines 35-47, you can simply do: if not x then ..., rather than doing if x then else ...
  • For checking if a value isn't nil, you can do if not x, rather than if x ~= nil, assuming the value you are trying to get isn't false, as both false and nil are falsey values

Hopefully this helped

Thank you for helping. One more thing , what line do i insert the code in Narrowlegobricks 6 — 10d
it would probably replace lines 29 - 47 theking48989987 1669 — 10d

Answer this question