So what im trying to do is make a racing system, i have the checkpoints,rewards,placing and stuff set up, but what is going on is when the race is finished, you get your rewards but then it decides to show the race countdown again and im not sure why
local queueSize = 9
local minPlayersNeeded = 1
local maxTime = 160
local countdown = 5
local text = "You are queued for a race"
local queuePart = workspace.RaceQueue
local queue=0
local placed = 0
local player
local car
local db = false
local disabled = false
local playersQueued = {}
local racing = {}
local rewards = {
[1] = 15000,
[2] = 8500,
[3] = 5000,
[4] = 1000
}
local raceInProgressBool = game:GetService("ReplicatedStorage").RaceInProgress
local function getGui(name)
local guiPossibility = game.Players:FindFirstChild(name)
if guiPossibility then
local gui = guiPossibility:WaitForChild("PlayerGui").GameInterface.QueuedGui
return gui
else
return nil
end
end
local function cleanUp()
queue=0
for i,v in pairs(playersQueued) do
playersQueued[i]=nil
v=nil
end
end
local function giveReward(place)
local reward = rewards[place]
if reward then
game.Players[player].leaderstats.Money.Value = game.Players[player].leaderstats.Money.Value + tostring(reward)
end
end
queuePart.Touched:Connect(function(hit)
if not db then
db = true
for _,queued in pairs(queuePart.Queue:GetChildren()) do
if hit.Parent.Name == "Body" or hit.Parent.Name == "Colour" then
if hit.Parent.Parent.Parent.DriveSeat.Owner.Value then
player = hit.Parent.Parent.Parent.DriveSeat.Owner.Value
end
if queued.Value ~= player then
table.insert(playersQueued,player)
queue=queue+1
queued.Value = player
local queueGui = getGui(player)
car = hit.Parent.Parent.Parent
if not disabled then
queueGui.queue.Text = text.." - "..queue.."/"..queueSize
else
queueGui.queue.Text = "Racing disabled due to issues."
end
break
end
end
end
wait(2)
db = false
end
end)
queuePart.TouchEnded:Connect(function(hit)
if not db and not disabled then
db = true
for _,queued in pairs(queuePart.Queue:GetChildren()) do
if hit.Parent.Name == "Body" or hit.Parent.Name == "Colour" then
playersQueued[player] = nil
player = hit.Parent.Parent.Parent.DriveSeat.Owner.Value
if queued.Value == player then
queue=queue-1
queued.Value = "none"
local queueGui = getGui(player)
car = hit.Parent.Parent.Parent
queueGui.queue.Text = ""
break
end
elseif hit.Parent.Name == "Wheel" then
for _,queued in pairs(queuePart.Queue:GetChildren()) do
if hit.Parent.Name == "Body" or hit.Parent.Name == "Colour" then
playersQueued[player] = nil
player = hit.Parent.Parent.Parent.DriveSeat.Owner.Value
if queued.Value == player then
queue=queue-1
queued.Value = "none"
local queueGui = getGui(player)
car = hit.Parent.Parent.Parent
queueGui.queue.Text = ""
break
end
end
end
end
end
wait(2)
db = false
else
local queueGui = getGui(player)
queueGui.queue.Text = ""
end
end)
local event
if not disabled then
for i,v in pairs(queuePart.Queue:GetChildren()) do
event = v.Changed:Connect(function()
local queuedPlayers = #playersQueued
if queuedPlayers >= minPlayersNeeded then
local _qG = getGui(v.Value)
for i = tonumber(countdown),0,-1 do
wait(1)
if #playersQueued >= minPlayersNeeded then
_qG.queue.Text = text.." - "..queue.."/"..queueSize.." ("..i.."s)"
else
event:Disconnect()
end
end
for i,v in pairs(playersQueued) do
table.insert(racing,v)
end
raceInProgressBool.Value = true
playersQueued[player] = nil
queue=0
game.ReplicatedStorage.Events.checkPoint:FireClient(game.Players[player])
local count = 1
local pCar
for _,playerCar in pairs(workspace[v.Value.." Vehicles"]:GetChildren()) do
if playerCar then
local plr = playerCar.DriveSeat.Occupant
pCar = playerCar
playerCar:SetPrimaryPartCFrame(workspace.Spawns[count].CFrame+Vector3.new(0,6,0))
playerCar:SetPrimaryPartCFrame(playerCar.PrimaryPart.CFrame * CFrame.Angles(math.rad(0),math.rad(90),math.rad(0)))
count=count+1
wait(.5)
end
end
local newCountdown = 4
local loopCountdown = coroutine.create(function()
while wait(1.5) do
newCountdown=newCountdown-1
print(newCountdown)
_qG.queue.Text = "("..newCountdown.."s)"
if newCountdown <= 0 then
_qG.queue.Text = "GO!"
coroutine.yield()
end
end
end)
coroutine.resume(loopCountdown)
for i,v in pairs(pCar.Wheels:GetChildren()) do
repeat wait() v.Anchored = true until newCountdown == 0
v.Anchored = false
wait(1)
for i = maxTime,0,-1 do
wait(1)
_qG.queue.Text = "You have ("..i.."s) remaining!"
end
for i,v in pairs(raceInProgressBool:GetDescendants()) do
if v and v.Name == player and v.Value == 1 then
giveReward(v.Place.Value)
_qG.queue.Text = "You have won in "..v.Place.Value.."st/th place!"
end
end
raceInProgressBool.Value = false
cleanUp()
event:Disconnect()
end
end
end)
end
end
local function createInstance(instance,parent,name)
local newInstance = Instance.new(instance)
newInstance.Parent = parent
newInstance.Name = name
return {i=newInstance}
end
local instances = {
["Folder"] = {a=raceInProgressBool,b="RacePlaces"}
}
raceInProgressBool:GetPropertyChangedSignal("Value"):Connect(function()
if raceInProgressBool.Value then
for i,instance in pairs(instances) do
local instanced = createInstance(i,instance.a,instance.b)
end
print("There are "..#racing.." people racing!")
for int,racer in pairs(racing) do
print("Int= "..int.."\nracer= "..racer)
for i,v in pairs(raceInProgressBool:GetChildren()) do
if v then
if v:IsA("Folder") then
local inst = createInstance("BoolValue",v,racer).i
local place = createInstance("IntValue",inst,"Place")
end
end
end
end
else
for i,v in pairs(raceInProgressBool:GetDescendants()) do
v:Destroy()
end
end
end)
game.ReplicatedStorage.Events.PlayerFinishedRace.OnServerEvent:Connect(function(plr)
if raceInProgressBool.Value then
for _,descendant in pairs(raceInProgressBool:GetDescendants()) do
if descendant and descendant.Name == plr.Name then
if placed < 4 then
placed=placed+1
else
placed=4
end
descendant.Place.Value = placed
end
end
end
end)