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 4 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:

local Backpack = Instance.new("Folder")
local player = game.Players.LocalPlayer
local BackpackStorageUsed = Instance.new("IntValue", Backpack)
local BackpackStorage = Instance.new("IntValue", Backpack)
local BackpackIndicator = game.StarterGui.BackpackGui.BackpackIndicator

Backpack.Name = "Backpack"
Backpack.Parent = player
BackpackStorageUsed.Name = "BackpackStorageUsed"
BackpackStorage.Name = "BackpackStorage"
BackpackStorage.Value =  10
BackpackStorageUsed.Value = 0

while wait() do
    script.Parent.Parent.StarterGui.BackpackGui.BackpackIndicator.Text = "Bag: " ..player.Backpack:FindFirstChild("BackpackStorageUsed").Value.. "/" ..player.Backpack:FindFirstChild("BackpackStorage").Value
end

Here's the other one:

local GiantBone = game.Workspace.GiantBone
local BackpackStorageUsed = game.Players.LocalPlayer.Backpack.BackpackStorageUsed
local BackpackStorage = game.StarterGui.BackpackGui.BackpackIndicator.BackpackStorage

GiantBone.ClickDetector.MouseClick:Connect(function()
    BackpackStorageUsed.Value = BackpackStorageUsed.Value + 1 

    if BackpackStorageUsed.Value ==  BackpackStorage.Value then
        GiantBone.ClickDetector.MaxActivationDistance = 0
    if BackpackStorageUsed.Value ==  BackpackStorageUsed.Value > BackpackStorage.Value then 
        GiantBone.ClickDetector.MaxActivationDistance = 32
    end
    end
end)
0
Is this being done in a ServerScript? Unhumanly 152 — 4y
0
Yes. TacticalTry 0 — 4y

2 answers

Log in to vote
1
Answered by 4 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.

GiantBone.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 4 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