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

Why is LocalPlayer a nil value?

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)

2 comments

0

Is this being done in a ServerScript?

0

Yes.

2 answers

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.

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)

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