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

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

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

3 comments

0

What line is it on?

0

line 35

0

c o d e b l o c k s ! ! ! !

1 answer

✓ Accepted answer

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:

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

2 comments

0

Thank you for helping. One more thing , what line do i insert the code in

0

it would probably replace lines 29 - 47

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