Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago
Edited 4 years ago

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 :)

01local button = script.Parent.Spawner
02local model = script.Parent.Car
03local backup = model:Clone()
04local debounce = false
05local model = script.Parent
06 
07local function onTouch(hit)
08    local h = hit.Parent:FindFirstChild("Humanoid")
09    if h ~= nil and debounce == false then
10        debounce = true
11        model = backup:Clone()
12        model.Parent = game.Workspace["Spawned Vehicles"]
13        model:MakeJoints()
14        wait(2)
15        debounce = false
16    end
17end
18 
19button.Touched:connect(onTouch)

1 answer

Log in to vote
0
Answered by 4 years ago

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:

01local base = script.Parent
02local car = workspace.QuoteUnquoteCar
03 
04local function left(part)
05 
06    if part.Name == car.name then
07 
08        print("CarHasLeftTheBase")
09 
10    end
11 
12end
13 
14base.TouchEnded:Connect(left)

2 Move part:

1while true do
2 
3    wait()
4    script.Parent.CFrame = script.Parent.CFrame * CFrame.new(0,1,0)
5    wait(3)
6    script.Parent.CFrame = script.Parent.CFrame * CFrame.new(0,-1,0)
7 
8end

Hope this helped!

0
Thank you so much man! I had no idea that TouchEnded existed. Thx :) blocky010101 23 — 4y
Ad

Answer this question