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

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

Asked by 5 years ago
game.Players.PlayerAdded:Connect(function(plr)

local a = Instance.new("Folder",plr)

a.Name = "leaderstats"

local m = Instance.new("IntValue",a)

m.Value = 0

m.Name = "Cash"

local x = Instance.new("IntValue",a)

x.Value = 0

x.Name = "Bananas"

end)





while true do

wait(1)

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

then

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

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

end

end

end

end
0
What line is it on? DeceptiveCaster 3761 — 5y
0
line 35 Narrowlegobricks 12 — 5y
0
c o d e b l o c k s ! ! ! ! LoganboyInCO 150 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years 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 Instance.new, 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

0
Thank you for helping. One more thing , what line do i insert the code in Narrowlegobricks 12 — 5y
0
it would probably replace lines 29 - 47 theking48989987 2147 — 5y
Ad

Answer this question