IntValue Not Working!

Hey Developers!
I am making a bus handler for my story game lobby, I plan to make a GUI That when players are in the bus, it will show how long it is before the players teleport, and how many players are in the bus, the Timer works, but the amount of players in the bus doesn’t show, when debugging, the output prints how many players, because I check when I add a player, but the while loop doesn’t add the amount of players to my intvalue!
Here is my script:

--Variables
local Bus = game.Workspace:WaitForChild('Bus')
local Seats = Bus.Seats
local Hitbox = game.Workspace:WaitForChild('Hitbox')

local ReplicatedStorage = game:GetService('ReplicatedStorage')
local TeleportService = game:GetService('TeleportService')
local ServerStorage = game:GetService('ServerStorage')
local Status = ReplicatedStorage:WaitForChild('Status')
local UI = ServerStorage:WaitForChild('ExitGui')
local BusPlayers = ReplicatedStorage:WaitForChild('BusPlayers')

local MinimumPlayers = 3
local Players = 0
local Time = 30
local Teleporting = false
local BusTable = {}

Hitbox.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild('Humanoid') and Teleporting == false then
		local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if table.find(BusTable, Player) == nil then
			for _, seat in pairs(Seats:GetChildren()) do
				if seat.Occupant == nil then
					table.insert(BusTable, Player)
					local Humanoid = Player.Character.Humanoid
					Humanoid.JumpPower = 0
					seat:Sit(Humanoid)
					UI:Clone().Parent = Player.PlayerGui
					Players = Players + 1
					print(Players)
					break
				end
			end
		end
	end
end)

ReplicatedStorage.LeaveBus.OnServerEvent:Connect(function(Player)
	local ReturnValue = table.find(BusTable, Player)
	table.remove(BusTable, ReturnValue)
	warn('Removed '..ReturnValue..' from the BusTable!')
	Players = Players - 1
	Player:LoadCharacter()
	Player.Character:WaitForChild('HumanoidRootPart').CFrame = game.Workspace.Setback.CFrame
end)

while true do
	wait()
	BusPlayers.Value = Players
	for i = Time, 1, -1 do
		Status.Value = i
		wait(1)
		if i == 1 and Players > 3 then
			--Teleport the Players!
		elseif i == 1 and Players < 3 then
			Status.Value = '6 Players Needed!'
			wait(1)
		end
	end
end

Thanks for any help, I don’t get any errors in the output!

I’m assuming your problem is that the BusPlayers’s value doesn’t update more than once. The reason is because you only set it before starting the countdown for loop.

But instead of setting it in a loop, why not set it every time you add/remove a player instead? That way it’s more efficient and doesn’t require any workarounds:

--Variables
local Bus = game.Workspace:WaitForChild('Bus')
local Seats = Bus.Seats
local Hitbox = game.Workspace:WaitForChild('Hitbox')

local ReplicatedStorage = game:GetService('ReplicatedStorage')
local TeleportService = game:GetService('TeleportService')
local ServerStorage = game:GetService('ServerStorage')
local Status = ReplicatedStorage:WaitForChild('Status')
local UI = ServerStorage:WaitForChild('ExitGui')
local BusPlayers = ReplicatedStorage:WaitForChild('BusPlayers')

local MinimumPlayers = 3
local Players = 0
local Time = 30
local Teleporting = false
local BusTable = {}

Hitbox.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild('Humanoid') and Teleporting == false then
		local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if table.find(BusTable, Player) == nil then
			for _, seat in pairs(Seats:GetChildren()) do
				if seat.Occupant == nil then
					table.insert(BusTable, Player)
					local Humanoid = Player.Character.Humanoid
					Humanoid.JumpPower = 0
					seat:Sit(Humanoid)
					UI:Clone().Parent = Player.PlayerGui
					Players = Players + 1
					BusPlayers.Value = Players --see here
					print(Players)
					break
				end
			end
		end
	end
end)

ReplicatedStorage.LeaveBus.OnServerEvent:Connect(function(Player)
	local ReturnValue = table.find(BusTable, Player)
	table.remove(BusTable, ReturnValue)
	warn('Removed '..ReturnValue..' from the BusTable!')
	Players = Players - 1
	BusPlayers.Value = Players --see here
	Player:LoadCharacter()
	Player.Character:WaitForChild('HumanoidRootPart').CFrame = game.Workspace.Setback.CFrame
end)

while true do
	wait()
	--no longer setting it here
	for i = Time, 1, -1 do
		Status.Value = i
		wait(1)
		if i == 1 and Players > 3 then
			--Teleport the Players!
		elseif i == 1 and Players < 3 then
			Status.Value = '6 Players Needed!'
			wait(1)
		end
	end
end

Furthermore, instead of incrementing/decrementing the Players variable to determine the players count, you can take advantage of the fact that you’re using a table (BusTable) to store the players on the bus. Lua has the # operator which allows you to get the length of a table, so you could change your code in the following way:

--Variables
local Bus = game.Workspace:WaitForChild('Bus')
local Seats = Bus.Seats
local Hitbox = game.Workspace:WaitForChild('Hitbox')

local ReplicatedStorage = game:GetService('ReplicatedStorage')
local TeleportService = game:GetService('TeleportService')
local ServerStorage = game:GetService('ServerStorage')
local Status = ReplicatedStorage:WaitForChild('Status')
local UI = ServerStorage:WaitForChild('ExitGui')
local BusPlayers = ReplicatedStorage:WaitForChild('BusPlayers')

local MinimumPlayers = 3
local Time = 30
local Teleporting = false
local BusTable = {}

Hitbox.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild('Humanoid') and Teleporting == false then
		local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if table.find(BusTable, Player) == nil then
			for _, seat in pairs(Seats:GetChildren()) do
				if seat.Occupant == nil then
					table.insert(BusTable, Player)
					local Humanoid = Player.Character.Humanoid
					Humanoid.JumpPower = 0
					seat:Sit(Humanoid)
					UI:Clone().Parent = Player.PlayerGui
					BusPlayers.Value = #BusTable --see here
					break
				end
			end
		end
	end
end)

ReplicatedStorage.LeaveBus.OnServerEvent:Connect(function(Player)
	local ReturnValue = table.find(BusTable, Player)
	table.remove(BusTable, ReturnValue)
	warn('Removed '..ReturnValue..' from the BusTable!')
	BusPlayers.Value = #BusTable --see here
	Player:LoadCharacter()
	Player.Character:WaitForChild('HumanoidRootPart').CFrame = game.Workspace.Setback.CFrame
end)

while true do
	wait()
	for i = Time, 1, -1 do
		Status.Value = i
		wait(1)
		local Players = #BusTable --see here
		if i == 1 and Players > 3 then
			--Teleport the Players!
		elseif i == 1 and Players < 3 then
			Status.Value = '6 Players Needed!'
			wait(1)
		end
	end
end
3 Likes