You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
I want to check if the bool value “Loaded” is true in the player’s backpack
What is the issue? Include screenshots / videos if possible!
The script stops on the if part
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
Tried to add the bool value directly to the player but also got the same error
The bool value “Loaded” is stored in the player’s backpack and this server script checks every player’s backpack and counts how many players have the bool value == true
for i, plrs in pairs(game.Players:GetPlayers()) do
local back = plrs.Backpack
if back.Loaded.Value == true then
c = c + 1
end
end
Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.
You can try adding edge-case checks and if they’re true assume said player hasn’t loaded(for example if their backpack or Loaded value doesn’t exist):
local function hasPlayerLoaded(player: Player): bool
if not player.Backpack then return false end
local val = player.Backpack:FindFirstChild("Loaded")
return val and val.Value
end
local function getLoadedPlayers(): {Player}
local players = {}
for _, player in pairs(game.Players:GetPlayers()) do
if hasPlayerLoaded(player) then table.insert(players, player) end
end
return players
end
local loadedPlayers = getLoadedPlayers()
print("Amount of players loaded:", #loadedPlayers)
Doesn’t quite work for me, loadedPlayers are always at 0
while wait() do
local function hasPlayerLoaded(player: Player): bool
if not player.Backpack then return false end
local val = player.Backpack:FindFirstChild("Loaded")
return val and val.Value
end
local function getLoadedPlayers(): {Player}
local players = {}
for _, player in pairs(game.Players:GetPlayers()) do
if hasPlayerLoaded(player) then table.insert(players, player) end
end
return players
end
local c = #getLoadedPlayers()
print(c)
wait(1)
end
Would it be better to put this in a separate script and make an int value in replicated storage and set it to be equal to #loadedPlayers? And then just keep checking the int value in the main script in a while loop
Ok so I made a remote event that gets triggered when the player loads and a server script, but it sends me a message its an infinite yield on the loaded bool?
game.ReplicatedStorage.Loaded.OnServerEvent:Connect(function(player)
print(player.Name.." is ready to duel")
local back = player:WaitForChild("Backpack")
back:WaitForChild("Loaded").Value = true
end)
Heres the code maybe I messed up something as simple as this since I’m very new
Ohhhhhh, I had to put the bool value in Starter Pack, I just directly made it into the backpack. It was an error due to my low experience. Thanks a lot!