Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Best way to detect when part moves away from other part?

I have made a car model, and I would like to respawn it when the car leaves its original location. I have a base part in the car which I want to be the part that is checked for when it leaves the invisible part at that location to mark where it is leaves. I would like to do this rather than with a manual button press (which I already have). How could I modify my current script to allow for this? I have tried while loops but they cause lag when constantly running and I don't have much experience with the Touched event and GetTouchingParts().

Thanks :)

local button = script.Parent.Spawner
local model = script.Parent.Car
local backup = model:Clone()
local debounce = false
local model = script.Parent

local function onTouch(hit)
	local h = hit.Parent:FindFirstChild("Humanoid")
	if h ~= nil and debounce == false then
		debounce = true
		model = backup:Clone()
		model.Parent = game.Workspace["Spawned Vehicles"]
		model:MakeJoints()
		wait(2)
		debounce = false
	end
end

button.Touched:connect(onTouch)

1 answer

✓ Accepted answer

In order to check if a part has stopped touching another you can use the TouchEnded event. It is important to know however, that both parts must be unanchored to do what you want using my method. Anchoring parts is not the only way to stop them from moving though, you can just weld them to another part that is anchored! THIS is a video showing how to make such a system for your vehicle.

These are the scripts if you are interested and want to copy and paste them:

#1 Check for touchEnded:

local base = script.Parent
local car = workspace.QuoteUnquoteCar

local function left(part)
	
	if part.Name == car.name then
		
		print("CarHasLeftTheBase")
		
	end
	
end

base.TouchEnded:Connect(left)

#2 Move part:

while true do
	
	wait()
	script.Parent.CFrame = script.Parent.CFrame * CFrame.new(0,1,0)
	wait(3)
	script.Parent.CFrame = script.Parent.CFrame * CFrame.new(0,-1,0)
	
end

Hope this helped!

1 comment

0

Thank you so much man! I had no idea that TouchEnded existed. Thx :)

This discussion is preserved as it was. New answers, comments, and votes are closed.