Timer Not Working

Hello, I am currently making a Story game, to be precise the lobby, and I am having some issues with a timer, so how my lobby works, is there is a bus, a player hits a part, and gets teleported into a seat, and then they get added into a table, and their walkspeed and jumppower gets set to 0. The player gets an exit UI, and another UI which shows them how long is left, and how many players are in the bus, I have a timer value and a player value in Replicated Storage.I also recieve this error, which I’ve never seen before: Screenshot by Lightshot
Here is my main script:

--Variables
local bus1Table = {}
local Bus1players = 0
local deb = false
local teleporting = false

game.Workspace.Teleport.TeleporterInfo1.Hitbox.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if hit.Parent:FindFirstChild('Humanoid') and teleporting == false then
		for _,seat in pairs(game.Workspace.Teleport.Bus1.Seats:GetChildren()) do
			if seat.Occupant == nil then
				local hum = player.Character.Humanoid
				hum.JumpPower = 0
				hum.WalkSpeed = 0
				player.Character.HumanoidRootPart.CFrame = seat.CFrame
				table.insert(bus1Table, player)
				warn(player.Name..' was added to the bus 1 Table')
				Bus1Players = Bus1Players + 1
				wait(3)
				print(Bus1Players)
			end
		end

		
	end
end)

game.ReplicatedStorage.LoadCharacter.OnServerEvent:Connect(function(player)
	player.Character.Humanoid.Sit = false
	wait()
	player:LoadCharacter()
	Bus1Players = Bus1Players - 1
end)

while true do
	game.ReplicatedStorage.Bus1Players.Value = Bus1Players
	wait()
end

while true do
wait()
for i = 30,-1 do
	game.ReplicatedStorage.Bus1Time.Value = i
	if i == 0 and Bus1Players >= 3 then
		print('Teleporting')
	else
		i = 30
	end
end
end  

And here is the amount shower

local PlayersInBus1 = game.ReplicatedStorage.Bus1Players
local Time = game.ReplicatedStorage.Bus1Time


while true do
	script.Parent.Text = 'Players: '..PlayersInBus1.Value..' | Time: '..Time.Value
	wait()
end

And here is the local script:

--Variables

local player = game.Players.LocalPlayer
local char = player.Character

--Main

script.Parent.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.LoadCharacter:FireServer(player)
	script.Parent.Parent:Destroy()
end)

Thanks for any help.
I don’t get any errors apart from this:

2 Likes

You define it as Bus1players yet:

You use Bus1Players, capitalisation matters!


Also,

This while loop is stopping the thread from reaching the while loop below, I’m sorta confused why you’re doing this though, couldn’t you just define Bus1Players as the NumberValue/IntValue and then edit upon the .Value etc?

1 Like

Your issue here seems to be a simple capitalization mistake. On line 3 of the first script listed above, change

local Bus1players = 0

to

local Bus1Players = 0
4 Likes

Ok. So that part seems to work now, but Im using this script:

while true do
	game.ReplicatedStorage.Bus1Players.Value = Bus1Players
	wait()
end

And the replicated storage Value isn’t changing.

1 Like

This is most likely because of two things:

  1. You have two while true do loops consecutively in the same script. The code will get stuck in the first loop and never reach the second one, which is where you are decrementing the value. Since this is never reached, your value will never decrement.
    A simple fix for the first issue is to start it in a new thread. An easy way to do this is wrap your first while loop in a spawned anonymous function, as such:

    spawn(function()
    	while true do
    		game.ReplicatedStorage.Bus1Players.Value = Bus1Players
    		wait()
    	end
    end)
    

    However, if you’re looking for the best code quality, I would recommend updating this value whenever you change it instead of constantly polling for changes.

  2. In the second while loop, you are only specifying two parameters of the for loop, which is not sufficient for counting down from 30. In the refactored code below, you’ll see I changed the parameters of the for loop in the second while loop to for i = 30, 0, -1 do so that it counts down from 30 to 0, decreasing by 1 each count.

Here is your script with some changes:

  • Using the # operator on a table to get the number of players, instead of dedicating a variable for it
  • Break out of the for loop once a seat has been found
  • Added -1 as the third parameter for the countdown loop so that it will not end instantly
  • Removed the first while loop and instead update the player counter object whenever it changes
  • Remove the player from the bus table when they reload
  • Use the :Sit() function of seats instead of teleporting the player’s character to the seat’s CFrame
  • Add :WaitForChild() to things to avoid race conditions of the script running before parts are loaded
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")

local bus1Players = {}
local debounce = false
local teleporting = false

local teleportPart = workspace:WaitForChild("Teleport")
local hitbox = teleportPart:WaitForChild("TeleporterInfo1"):WaitForChild("Hitbox")
local bus1SeatsModel = teleportPart:WaitForChild("Bus1"):WaitForChild("Seats")

local bus1PlayersCounter = ReplicatedStorage:WaitForChild("Bus1Players")
local bus1Time = ReplicatedStorage:WaitForChild("Bus1Time")

hitbox.Touched:Connect(function(hit)
	if debounce then return end
	debounce = true
	
	if teleporting then return end

	local player = Players:GetPlayerFromCharacter(hit.Parent)
	if not player then return end
	
	local humanoid = player.Character and player.Character:FindFirstChild("Humanoid")
	if not humanoid then return end

	local rootPart = player.Character.PrimaryPart
	if not rootPart then return end

	for _, seat in pairs(bus1SeatsModel:GetChildren()) do
		if not seat.Occupant then
			humanoid.JumpPower = 0
			humanoid.WalkSpeed = 0
			seat:Sit(humanoid)
			table.insert(bus1Players, player)
			bus1PlayersCounter.Value = #bus1Players
			warn(player.Name .. " was added to the bus 1 Table")
		end
	end
	
	wait(.1)
	debounce = false
end)

game.ReplicatedStorage.LoadCharacter.OnServerEvent:Connect(function(player)
	local character = player.Character or player.CharacterAdded:Wait()
	local humanoid = character:WaitForChild("Humanoid")
	humanoid.Sit = false
	player:LoadCharacter()
	for index, busPlayer in pairs(bus1Players) do
		if player.UserId == busPlayer.UserId then
			table.remove(bus1Players, index)
			bus1PlayersCounter.Value = #bus1Players
		end
	end
end)

while true do
	for i = 30, 0, -1 do
		bus1Time.Value = i
		wait(1)
	end
	if #bus1Players >= 3 then
		teleporting = true
		print("Teleporting")
		-- do your teleportations
		-- maybe reset bus1Players = {}?
		teleporting = false
	end
end
2 Likes

Wow!
Thank you so much!..

1 Like

Alright… So I tried it out, but it doesn’t work, I don’t get any players in the output, the timer works, but when the player hit the hitbox, it prints Hit, but the player doesn’t get teleported to a seat. Here is the script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")

local bus1Players = {}
local debounce = false
local teleporting = false

local teleportPart = workspace:WaitForChild("Teleport")
local hitbox = teleportPart:WaitForChild("TeleporterInfo1"):WaitForChild("Hitbox")
local bus1SeatsModel = teleportPart:WaitForChild("Bus1"):WaitForChild("Seats")

local bus1PlayersCounter = ReplicatedStorage:WaitForChild("Bus1Players")
local bus1Time = ReplicatedStorage:WaitForChild("Bus1Time")

hitbox.Touched:Connect(function(hit)
	print('Hit')
	if debounce then return end
	debounce = true
	
	if teleporting then return end

	local player = Players:GetPlayerFromCharacter(hit.Parent)
	if not player then return end
	
	local humanoid = player.Character and player.Character:FindFirstChild("Humanoid")
	if not humanoid then return end

	local rootPart = player.Character.PrimaryPart
	if not rootPart then return end

	for _, seat in pairs(bus1SeatsModel:GetChildren()) do
		if not seat.Occupant then
			humanoid.JumpPower = 0
			humanoid.WalkSpeed = 0
			seat:Sit(humanoid)
			table.insert(bus1Players, player)
			bus1PlayersCounter.Value = #bus1Players
			warn(player.Name .. " was added to the bus 1 Table")
		end
	end
	
	wait(.1)
	debounce = false
end)

game.ReplicatedStorage.LoadCharacter.OnServerEvent:Connect(function(player)
	local character = player.Character or player.CharacterAdded:Wait()
	local humanoid = character:WaitForChild("Humanoid")
	humanoid.Sit = false
	player:LoadCharacter()
	for index, busPlayer in pairs(bus1Players) do
		if player.UserId == busPlayer.UserId then
			table.remove(bus1Players, index)
			bus1PlayersCounter.Value = #bus1Players
		end
	end
end)

while true do
	for i = 30, 0, -1 do
		bus1Time.Value = i
		wait(1)
	end
	if #bus1Players >= 3 then
		teleporting = true
		print("Teleporting")
		-- do your teleportations
		-- maybe reset bus1Players = {}?
		teleporting = false
	end
end
1 Like