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