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

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

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:

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!

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

Answer this question