Struggling with teleporting a model out of another part to a different location

  1. What do you want to achieve? Keep it simple and clear!
    I’m trying to make a system that moves a player’s kart back to the last checkpoint they’ve passed through if they touch a part marked as “out of bounds”. The movement part works fine, but there’s a slight issue.

  2. What is the issue? Include screenshots / videos if possible!
    The system works mostly as intended, but I’ve encountered a problem where the kart won’t trigger the system if a specific zone has already been touched before.

Manually driving in and out of the zone once remedies the problem, so I assume the issue is that I’m using model:PivotTo() and it’s making the kart not register that it has stopped touching the out of bounds area.

Here’s a diagram of what I’m trying to achieve:

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve tried tweening the kart’s CFrame and moving it via part.Position = (X, Y, Z), but both of them ended up breaking the kart. I don’t have any more ideas and the issue is heavily hindering the development of my game.

Here’s a watered down version of the code that handles out of bounds collisions:

--"kart" is the main body part of the kart, not the whole model.
--"kart.Parent" is the model containing the kart's parts, including the frame and wheels.
--"respawnCFrame" is the rotation/center location of the last checkpoint the kart has touched.
--"isFloating" defines whether or not the kart is in the process of moving automatically so that nothing else triggers.

local function col(box)
	local boxParent = box.Parent
	local outOfBounds = boxParent:FindFirstChild("COL_BOUNDARY")

	if outOfBounds then
		if isFloating == false then
			kart.Anchored = true
			kart.HitSound.Playing = true
			isFloating = true
			
			driverPlayer.PlayerGui.RaceHUD.ScreenFade.OOBAnimation:FireClient(driverPlayer) --This line isn't really important, it just fires the UI animation for driving out of bounds.
			kart.Parent.PrimaryPart = kart
			wait(0.5)
			kart.AssemblyLinearVelocity = Vector3.new(0, 0, 0)
			kart.AssemblyAngularVelocity = Vector3.new(0, 0, 0)
			kart.Parent:PivotTo(respawnCFrame)
			isFloating = false
			wait(1)
			kart.Anchored = false
			kart.Parent.PrimaryPart = nil
		end
	end
end

kart.Touched:Connect(col)

There are a lot of advanced concepts in Lua that I struggle to understand, so explanations are encouraged!

5 Likes

Is it possible that when you move the kart back to a checkpoint, it is still in or touching the out-of-bounds zone? If that is the case, kart.Touched:Connect(col) wouldn’t trigger.

Yeah, that’s the issue. I’m trying to figure out a way to move the kart out of the OOB zone while making sure that the kart actually detects that it has done so.

You can possibly create boxes inside where the tracks are, and if the game detects the player isnt in any box anymore, teleport them to the last checkpoint

I thought about that approach at one point, but I feel as though it’d be really inconvenient due to how time-consuming it’d be to set up on highly complex tracks.

I’ll keep it in mind as a last resort, however.

Have you tried using print statements to check if out-of-bounds and isfloating are the values they should be?

I have, and like I said, the issue is the fact that I’m teleporting the kart out of an OOB area by using :PivotTo(), which makes the kart think it’s still touching the OOB part.

What I’m trying to figure out is how I can move the kart out of said region while also triggering TouchEnded.

You can also try replacing PivotTo with SetPrimaryPartCFrame but then kart.Parent.PrimaryPart would have to always be equal to kart and you need to not set it to nil

If the cart has a primary part thst everytime is welded to, just by moving the primarypart it should move, how come we are using picot to instead of cframes

Edit : exactly like happyfloof just said

You have to use model:SetPrimaryPartCFrame(cframe)

If model:SetPrimaryPartCFrame(CFrame) doesn’t work, make sure you have a primarypart set, and don’t ever set it to nil

If this doesn’t work, can you also send the part of the code where respawnCFrame is defined. And if possible, can you send more of the script?

I tried what both of you have said, but again, what I’m trying to do is move the kart out of the OOB area while making sure that TouchEnded is triggered.

SetPrimaryPartCFrame gives the same result as before.

Is COL_BOUNDARY getting deleting from the parts?

Could try using a variable such as moved and check if its true, which it would be true from moving the kart, then run whatever you need as a workaround to touchended.

No parts are added or removed in the process, the kart is simply failing to register that it has stopped touching it when teleported.

Can try adding a function under the already existing function that can check if outofbounds is false, should only be triggered if the outofbounds was initially true from before

Also, as a last resort, you can try using GetTouchingParts

or set a loop like

while wait(0.5) do
   workspace:GetPartsInPart(part, OverlapParams.new())
end

inside of the karts main hitbox or just using the main body

Also you should put print statements throughout your code to see what part of it is firing and where does it stop so you can fix it.

the “if outOfBounds” part of the code only triggers when box.Name is COL_BARRIER, for the rest of that function’s runtime it never gets set back to nil, so that wouldn’t work

I’ve print debugged my whole script several times. I already know what the issue is: