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

Why is LocalPlayer a nil value?

Asked by 5 years ago

So yeah, I was trying to make a backpack storage Gui. Obviously, I tried to put the two values (BackpackStorageUsed, BackpackStorage) into a folder inside the player because everyone would have different used amount of storage. So, I tried it and in the Output it said:

09:04:08.890 - ServerScriptService.Script:15: attempt to index local 'player' (a nil value)

Can anyone help? I have two scripts; one ClickDetector script so that when I click it, the BackpackStorageUsed value will go up by one, and the other is to keep updating the Gui and to create all the values when the player joins:

01local Backpack = Instance.new("Folder")
02local player = game.Players.LocalPlayer
03local BackpackStorageUsed = Instance.new("IntValue", Backpack)
04local BackpackStorage = Instance.new("IntValue", Backpack)
05local BackpackIndicator = game.StarterGui.BackpackGui.BackpackIndicator
06 
07Backpack.Name = "Backpack"
08Backpack.Parent = player
09BackpackStorageUsed.Name = "BackpackStorageUsed"
10BackpackStorage.Name = "BackpackStorage"
11BackpackStorage.Value =  10
12BackpackStorageUsed.Value = 0
13 
14while wait() do
15    script.Parent.Parent.StarterGui.BackpackGui.BackpackIndicator.Text = "Bag: " ..player.Backpack:FindFirstChild("BackpackStorageUsed").Value.. "/" ..player.Backpack:FindFirstChild("BackpackStorage").Value
16end

Here's the other one:

01local GiantBone = game.Workspace.GiantBone
02local BackpackStorageUsed = game.Players.LocalPlayer.Backpack.BackpackStorageUsed
03local BackpackStorage = game.StarterGui.BackpackGui.BackpackIndicator.BackpackStorage
04 
05GiantBone.ClickDetector.MouseClick:Connect(function()
06    BackpackStorageUsed.Value = BackpackStorageUsed.Value + 1
07 
08    if BackpackStorageUsed.Value ==  BackpackStorage.Value then
09        GiantBone.ClickDetector.MaxActivationDistance = 0
10    if BackpackStorageUsed.Value ==  BackpackStorageUsed.Value > BackpackStorage.Value then
11        GiantBone.ClickDetector.MaxActivationDistance = 32
12    end
13    end
14end)
0
Is this being done in a ServerScript? Unhumanly 152 — 5y
0
Yes. TacticalTry 0 — 5y

2 answers

Log in to vote
1
Answered by 5 years ago

LocalPlayer can inly be used locally. An outcome of MouseClick is it gives you the player that clicked it like PlayerAdded does, so you can set up the start of your function like this.

1GiantBone.ClickDetector.MouseClick:Connect(player)

This means you wont be able to define the backpack etc before you call the function, just do it in the function.

Ad
Log in to vote
0
Answered by 5 years ago

I had the same problem, and sometimes I still have this problem, but in order to fix this, you have to put in the same code and put it in a local script. (Make sure the local script is in StarterPlayer)

Answer this question