Need help instancing a value in the player themselves

I’m trying to instance a BoolValue I placed in the player (using Instance.new), but this error shows up:
ServerScriptService.TestScript:10: attempt to index nil with 'localvalues’

Here’s my code:

game.Players.PlayerAdded:Connect(function(plr)
	local folder = Instance.new("Folder",plr)
	folder.Name = "localvalues"
	
	local bool = Instance.new("BoolValue",folder)
	bool.Name = "test"
	bool.Value = true
end)

if game.Players.LocalPlayer.localvalues.test.Value == true then
	print("Success")
end

May someone point out what’s wrong?

is it created in a local script? this is most likely the reason.

Yeah, instead of saying:
if game.Players.LocalPlayer.localvalues.test.Value == true

end
Point out a specific player by username,userid,variable, or a unique characteristic. You can also just use a for loop to loop through all players.

2 Likes

No, it’s a regular script placed in ServerScriptService.

1 Like

And how do I do that? (P.S. I’m a hecka new scripter)

1 Like

This should be replaced by:

if game.Players.*aPlayerName*.localvalues.test.Value == true then
	print("Success")
end

Pointing out that LocalPlayer only exists on the client, not the server. That is why it shows nil.

You could also place all of this into the PlayerAdded function.

game.Players.PlayerAdded:Connect(function(plr)
	local folder = Instance.new("Folder",plr)
	folder.Name = "localvalues"

	local bool = Instance.new("BoolValue",folder)
	bool.Name = "test"
	bool.Value = true
	
	if plr.localvalues.test.Value == true then
		
		print("Success")
		
	end
end)
1 Like

Does this mean I can do this only for one player?

1 Like

It depends. Do you need this for every player?

1 Like

Yes. I do need this for every player.

1 Like

Well, if you need to do this outside of the PlayerAdded function, then sth like this could work:

local players = game:GetService("Players")
for i, player in pairs(players:GetChildren()) do -- loop through every player
	
	local value = player:FindFirstChild("localvalues").test.Value -- find out the value
	if value then -- if the value is true, continue
		
		print("Success!")
		
	end
	
end

All it does, is create a for loop, that loops through all of the players in the server, and checks if they have the values.

It works alright, no errors, but “Success!” doesn’t get printed.
Here’s the code:

game.Players.PlayerAdded:Connect(function(plr)
	local folder = Instance.new("Folder",plr)
	folder.Name = "localvalues"
	
	local bool = Instance.new("BoolValue",folder)
	bool.Name = "test"
	bool.Value = true
end)

local players = game:GetService("Players")
for i, player in pairs(players:GetChildren()) do -- loop through every player

	local value = player:FindFirstChild("localvalues").test.Value -- find out the value
	if value then -- if the value is true, continue
		print("Success!")
	end
end

[If I understood your issue correctly].

game.Players.PlayerAdded:Connect(function(plr)
	local folder = Instance.new("Folder")
	folder.Name = "localvalues"
	folder.Parent = plr

	local bool = Instance.new("BoolValue")
	bool.Name = "test"
	bool.Value = true
    bool.Parent = folder
end)

local players = game:GetService("Players")

for i, player in pairs(players:GetPlayers()) do
	local value = player:FindFirstChild("localvalues").test.Value -- find out the value
	if value== true  then 
		print("Success!")
    else 
        print("failed!")
	end
end

Notes:

Never put the parent as an argument.
that loop will run only once.

2 Likes

I think you missed something. This already gets the value. There is a .Value at the end.

local value = player:FindFirstChild("localvalues").test.Value
1 Like

Yes, I just noticed right now .

1 Like

Try this

--Variables
local players = game:GetService("Players")

--Functions

local function OnChangedValue(Value)
	if Value.Value== true  then 
		print("Success!")
	else 
		print("failed!")
	end
end

local function checkValue()
	for i, player in pairs(players:GetPlayers()) do
		local value = player:FindFirstChild("localvalues").test.Value -- find out the value
		if value== true  then 
			print("Success!")
		else 
			print("failed!")
		end
	end
end

local function OnPlayerArrival(plr)
	local folder = Instance.new("Folder")
	folder.Name = "localvalues"
	folder.Parent = plr

	local bool = Instance.new("BoolValue")
	bool.Name = "test"
	bool.Value = true
	bool.Parent = folder
	
	checkValue()
	
	bool.Changed:Connect(function()
		OnChangedValue(bool)
	end)
end
--Logic
players.PlayerAdded:Connect(OnPlayerArrival)

This will:
-Detect the initial value of their value and print success or failed.
-Detect when that value changes and prints accordingly.

1 Like

Works perfectly fine mate, tysm!

1 Like

One last thing, if I wanna change the value of the thing later, how would I do that?