Trying to make a "while touching reload" and break that while loop if the object stops touching

hey all,

I have this reload/refuel part for my planes. If the vehicle touches it, the player must wait two seconds while touching the part with their engine off for the reload/refueling process to begin. While the touch is true, the guns are immediately fully loaded, and the fuel and health replenishment begins in increments.

Problem is, is that this does not stop even if the vehicle stops touching the part.
Anyone know how to solve this?

edit: ok so re-iteration a little, but it is still a little raw. I don’t know if the code will be able to handle multiple objects at once

local supplyteam = "Blue"
local touching
local tag
function status(Object, tag)
	if Object and tag == "here" then
		touching = true
	end
	if Object and tag == "gone" then
		touching = false
	end
end
script.Parent.Touched:Connect(function(Object)
	tag = "here"
	status(Object, tag)
	if Object:IsA("BasePart") then
		local seatfind = Object.Parent.Parent:FindFirstChild("PlaneSeat")
		if seatfind ~= nil then
			local planeteam = Object.Parent.Parent:FindFirstChild("Team")
			local values = Object.Parent.Parent:FindFirstChild("Values")
			local Hp = Object.Parent.Parent.Hp
			if planeteam.Value == supplyteam then
				if values.Active.Value == false then
					while touching == true do
						wait(2)
						values.Ammo.Bullets.Value = values.Ammo.Bullets.MaxBullets.Value
						values.Ammo.Bombs.Value = values.Ammo.Bombs.MaxBombs.Value
						values.Ammo.Missiles.Value = values.Ammo.Missiles.MaxMissiles.Value
						if values.Fuel.Value >= 93 and values.Fuel.Value <= 99 or values.Fuel.Value == 100 then
							values.Fuel.Value = 100
						else
							values.Fuel.Value = values.Fuel.Value + 8
						end
						if Hp.Value >= Hp.Maxhp.Value - 59 and Hp.Value <= Hp.Maxhp.Value - 1 or Hp.Value == Hp.Maxhp.Value then
							Hp.Value = Hp.Maxhp.Value
						else
							Hp.Value = Hp.Value + 60
						end
						if values.Turret then
							local children = values.Turret:GetChildren()
							for i = 1,#children do
								if children[i].Name == "GunTurret" then
									if children.FiringGun.Value == false then
										i.Bullets.Value = i.Bullets.MaxBullets.Value
									end
								end
							end
						end
					end
					
				end
			end
		end
	end
end)
script.Parent.TouchEnded:connect(function(Object)
	tag = "gone"
	status(Object, tag)
end)

Thanks!

1 Like

Instead of the touched and touchended event, you should use a proximity test for a certain part on the plane, then run the test before and after the 2 second wait.

Use this distance check function:

function dist(p1, p2)
return (p1 - p2).Magnitude
end

(Sorry for the bad layout I’m on iPhone)

is there a way to just to touched? I don’t want to just do proximity for a y coordinate, I thought a reload part that you touch would be better.

The proximity function takes p1, and p2, both Vector3’s.

You could use a large CanCollide off invisible part that detects a touched event specifically on one part of the plane. Then use the touch ended event to detect when the plane leaves.

this is kinda what i did in the code

but how is this an advantage to the touched event

because my problem is a delay from stopping the reload sequence when the player leaves