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 :)
01 | local button = script.Parent.Spawner |
02 | local model = script.Parent.Car |
03 | local backup = model:Clone() |
04 | local debounce = false |
05 | local model = script.Parent |
06 |
07 | local 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 |
17 | end |
18 |
19 | button.Touched:connect(onTouch) |
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:
01 | local base = script.Parent |
02 | local car = workspace.QuoteUnquoteCar |
03 |
04 | local function left(part) |
05 |
06 | if part.Name = = car.name then |
07 |
08 | print ( "CarHasLeftTheBase" ) |
09 |
10 | end |
11 |
12 | end |
13 |
14 | base.TouchEnded:Connect(left) |
1 | while 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 |
8 | end |
Hope this helped!