Bool value backpack error

You can write your topic however you want, but you need to answer these questions:

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

  2. What is the issue? Include screenshots / videos if possible!

    The script stops on the if part

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


Here’s an image of the print, its the lowest one which got printed 41 times

Maybe the server can’t see the Loaded value, are you creating/setting it on the client?

Yea, I’m setting it in a local script. Forgot that the server can’t see that, I’ll reply about how it goes

1 Like

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?


image

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

You must also create the value on the server.

1 Like

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!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.