Hello everyone! I’m scripting for the first time and following AlvinBlox’s tutorial on how to make a story game. However, I wanted to make some changes, add my own custom stuff etc along the way so I could learn better.
The timer is supposed to only count down if a person is in the truck, and this sorta works, but when they leave, the timer still counts down! How do i fix this and why is it acting like this?
local TeleportService = game:GetService("TeleportService")
local SignLabel = script.Parent.Parent.Sign.Top.SurfaceGui.TextLabel
while true do
local players = {}
for i, v in pairs(script.Parent.Seats:GetChildren()) do
if v.Occupant then
print("Occupant found. "..v.Parent.Name)
table.insert(players,game.Players:GetPlayerFromCharacter(v.Occupant.Parent)) -- As the Occupant value is the Humanoid, inside of character (v)
end
end
if #players > 0 then
for i = 60,0,-1 do
SignLabel.Text = "Leaving in "..i.." seconds"
task.wait(1)
end
for i = 0,150,1 do -- Change 30 to number of studs
script.Parent:TranslateBy(script.Parent.Hitbox.CFrame.lookVector)
task.wait(0.01)
end
local serverData = TeleportService:ReserveServer(101045119784698) -- Gives an access code needed to get into the Private Server
TeleportService:TeleportToPrivateServer(101045119784698,serverData,players)
for i = 0,150,1 do -- Change 30 to number of studs
script.Parent:TranslateBy(-script.Parent.Hitbox.CFrame.lookVector)
task.wait(0.01)
end
else do
SignLabel.Text = "Leaving in 60 seconds"
end
end
-- Move truck
-- Reserve server
task.wait(1)
end
You need to check if a player is inside the truck in the countdown’s for loop and if there isn’t, break the loop
for i = 60,0,-1 do
if not <Check whether there are players in the truck> then SignLabel.Text = "Waiting for Players" break end
SignLabel.Text = "Leaving in "..i.." seconds"
task.wait(1)
end
Since you are creating a table with all the players you can just check whether # player is more than zero but then you’ll also have to update the players table in the loop(so basically you just have to also put the first few lines of the while loop into the for loop)
So one way you could do this would be:
local TeleportService = game:GetService("TeleportService")
local SignLabel = script.Parent.Parent.Sign.Top.SurfaceGui.TextLabel
while true do
local players = {}
for i, v in pairs(script.Parent.Seats:GetChildren()) do
if v.Occupant then
print("Occupant found. "..v.Parent.Name)
table.insert(players,game.Players:GetPlayerFromCharacter(v.Occupant.Parent)) -- As the Occupant value is the Humanoid, inside of character (v)
end
end
if #players > 0 then
for i = 60,0,-1 do
players = {}
for i, v in pairs(script.Parent.Seats:GetChildren()) do
if v.Occupant then
print("Occupant found. "..v.Parent.Name)
table.insert(players,game.Players:GetPlayerFromCharacter(v.Occupant.Parent))
if not (#players > 0) then SignLabel.Text = "Waiting for players" break end
SignLabel.Text = "Leaving in "..i.." seconds"
task.wait(1)
end
for i = 0,150,1 do -- Change 30 to number of studs
script.Parent:TranslateBy(script.Parent.Hitbox.CFrame.lookVector)
task.wait(0.01)
end
if #players > 0 then -- check whether the countdown has been stopped
local serverData = TeleportService:ReserveServer(101045119784698) -- Gives an access code needed to get into the Private Server
TeleportService:TeleportToPrivateServer(101045119784698,serverData,players)
for i = 0,150,1 do -- Change 30 to number of studs
script.Parent:TranslateBy(-script.Parent.Hitbox.CFrame.lookVector)
task.wait(0.01)
end
end
else do
SignLabel.Text = "Leaving in 60 seconds"
end
end
-- Move truck
-- Reserve server
task.wait(1)
end
Though this code isn’t optimized and might be broken due to typos since I can’t test it mysekd because I don’t have access to my Pc right now. But I will look for a better way as soon as I can access my Pc again.
Hey, thanks for the help. I tried your code but it didn’t work, not sure in what manner it ended up not working since it’s been hours but I just kept making adjustments and it’s finally working as intended. Not too sure how clean or performant this is buut it works
local TeleportService = game:GetService("TeleportService")
local SignLabel = script.Parent.Parent.Sign.Top.SurfaceGui.TextLabel
while true do
local players = {}
for i, v in pairs(script.Parent.Seats:GetChildren()) do
if v.Occupant then
local plr = game.Players:GetPlayerFromCharacter(v.Occupant.Parent)
print(#players)
if plr then
table.insert(players,plr)
end-- As the Occupant value is the Humanoid, inside of character (v)
end
end
if #players > 0 then
for i = 30,0,-1 do
players = {}
for i, v in pairs(script.Parent.Seats:GetChildren()) do
if v.Occupant then
local plr = game.Players:GetPlayerFromCharacter(v.Occupant.Parent)
if plr then
table.insert(players,plr)
end
print(#players)
print(players)
end
end
if not (#players > 0) then
SignLabel.Text = "Waiting for players"
break
end
SignLabel.Text = "Leaving in "..i.." seconds"
task.wait(1)
end
if #players > 0 then
for i = 0,150,1 do -- Change 30 to number of studs
script.Parent:TranslateBy(script.Parent.Hitbox.CFrame.lookVector)
task.wait(0.01)
end
local serverData = TeleportService:ReserveServer(101045119784698) -- Gives an access code needed to get into the Private Server
TeleportService:TeleportToPrivateServer(101045119784698,serverData,players)
for i = 0,150,1 do -- Change 30 to number of studs
script.Parent:TranslateBy(-script.Parent.Hitbox.CFrame.lookVector)
task.wait(0.01)
end
end
else do
SignLabel.Text = "Waiting for players"
end
end
-- Move truck
-- Reserve server
task.wait(1) -- yield
end