Cframe regen system not updating when vehicle leaves area (video example)

Hey fellas. Below I have a regen script for my plane system and a video example of what i want and what’s going on.

Expectation:

.
.
.
Reality:

.
.
.
The problem if you didn’t understand:
In the first video you can see that I regen the plane, and a countdown begins. If i leave the area the countdown ends and the regen button reappears. In the first video you can also see me wait for the countdown to end, and when it does, the regen button reappears and i regen the plane, and my previous plane is destroyed.

Everything works in my replica except that the countdown fails to stop even though I left the area. Why?
.
.
Code:

local Range = 10
local Spawn = script.Parent.Spawn
local button = script.Parent.Button
local investment = button.Investment.Value
local fixedprice = button.Fixedprice.Value
local price = button.Price.Value
local blue = game.Teams.Blue
local red = game.Teams.Red
local board = game.ReplicatedStorage.Board
local bsecurity = board.Blue.Misc.SecurityLevel.Value
local rsecurity = board.Red.Misc.SecurityLevel.Value
local name = button.Displayname.TextLabel
local label = button.Cooldown.TextLabel
local Vehicle = script.Parent.BlueFighter
local OldVehicle = Vehicle:Clone()
Vehicle:Destroy()

db1 = true
function regenerate()
if not db1 then
   return
else
   db1 = false
   local Vehiclesinspawn = script.Parent:GetChildren()
   for i = 1,#Vehiclesinspawn do
   	if Vehiclesinspawn[i].Name == "BlueFighter" then
   		if (Vehicle.PlaneSeat.CFrame.p-Spawn.CFrame.p).magnitude <= Range then
   			Vehicle:Destroy()
   			end
   		end
   	end
   Vehicle = OldVehicle:Clone()
   Vehicle.Parent = script.Parent
   	while (Vehicle.PlaneSeat.CFrame.p-Spawn.CFrame.p).magnitude >= Range do
   		if (Vehicle.PlaneSeat.CFrame.p-Spawn.CFrame.p).magnitude <= Range then
   			label.TextTransparency = 1
   			name.TextTransparency = 0
   			button.Transparency = 0
   			button.CanCollide = true
   			break
   		end
   		label.TextTransparency = 0
   		button.Transparency = 1
   		name.Transparency = 1
   		button.CanCollide = false
   		label.Text = 15
   		wait(1)
   		label.Text = 14
   		wait(1)
   		label.Text = 13
   		wait(1)
   		label.Text = 12
   		wait(1)
   		label.Text = 11
   		wait(1)
   		label.Text = 10
   		wait(1)
   		label.Text = 9
   		wait(1)
   		label.Text = 8
   		wait(1)
   		label.Text = 7
   		wait(1)
   		label.Text = 6
   		wait(1)
   		label.Text = 5
   		wait(1)
   		label.Text = 4
   		wait(1)
   		label.Text = 3
   		wait(1)
   		label.Text = 2
   		wait(1)
   		label.Text = 1
   		wait(1)	
   		label.Text = 0
   		wait(1)
   		label.TextTransparency = 1
   		name.TextTransparency = 0
   		button.Transparency = 0
   		button.CanCollide = true
   		break	
   	end
   end
db1 = true	
end

db = true
function OnTouched(hit)
   if not db then
   	return
   else
   	db = false
   	if (hit.Parent:FindFirstChild("Humanoid")) then
   		local player = game.Players:FindFirstChild(hit.Parent.Name)
   		if player.TeamColor == blue or rsecurity >= bsecurity then
   			local cash = player.leaderstats.Cash
   			if (cash.Value >= 1 and investment + cash.Value < price or investment + cash.Value < fixedprice) then
   				investment = investment + cash.Value
   				cash.Value = 0
   				price = fixedprice - investment
   				name.Text = ("Buy Fighter: " .. price .. " Tech Points")			
   				print(price)
   				wait(1)
   				db = true			
   			else
   				if (cash.Value + investment >= price) then
   					price = fixedprice - investment
   					cash.Value = cash.Value - price
   					price = fixedprice
   					investment = 0	
   					regenerate()
   					name.Text = ("Buy Fighter: 10 Tech Points")	
   					wait(1)
   					db = true
   				end
   			end
   		end
   	end
   end
end
button.Touched:Connect(OnTouched)

Your countdown is linear. There’s nothing actually checking whether the plane is out of distance inbetween each second. Create a for loop that checks if the plane is out of the area then make it cut the countdown short.

local i = 0
i = 15
while (Vehicle.PlaneSeat.CFrame.p-Spawn.CFrame.p).magnitude >= Range do
	if i >= 1 then
		label.TextTransparency = 0
		button.Transparency = 1
		name.Transparency = 1
		button.CanCollide = false   
		i = i - 1
		label.Text = i
		if (Vehicle.PlaneSeat.CFrame.p-Spawn.CFrame.p).magnitude <= Range then
			label.TextTransparency = 1
			name.TextTransparency = 0
			button.Transparency = 0
			button.CanCollide = true
			break
		end
		wait(1)
	end
	if i == 0 then
		label.TextTransparency = 1
		name.TextTransparency = 0
		button.Transparency = 0
		button.CanCollide = true
		break
	end
end

??

nvm, got it working, boss. thanks.

1 Like