Trying to teleport a model but it doesnt work

Hello there! Some time ago (about a month back), I was working on a chest script with a simple concept: teleport to one of the positions and if you make contact with it, you win! However, it didn’t work as expected. I’m still in the process of refining the script to get it functioning properly. Interestingly, no error messages are being displayed on my end.

Here’s the script I have ( its the teleporting script ):

local spawns = workspace.Spawns:GetChildren()
local Chest = game.Workspace.Chest

local function onTouch(part)
	local humanoid = part.Parent:FindFirstChildOfClass("Humanoid")
	if not humanoid then
		return
	end
	
	local chest = part.Parent:FindFirstChild("ChestRandom")
	if not chest then
		return
	end

	-- Choose a random spawn position
	local randomSpawnIndex = math.random(1, #spawns)
	local randomSpawn = spawns[randomSpawnIndex]
	local targetPosition = randomSpawn.Position

	chest:MoveTo(targetPosition)
end

local triggerPart = Chest
triggerPart.Touched:Connect(onTouch)

Regarding the absence of an “end” statement, it’s because the script is designed to run indefinitely, unless someone leaves the server, which would then close the script’s execution.

6 Likes

Is there an object called “ChestRandom” inside the player? If not, then this would end the function because since there is no “ChestRandom” inside the player, the variable chest would be nil, and if not chest then would become true and it would return.

To fix this, you would need to change it to if chest then return end.

5 Likes

Exactly this. If that isn’t the case, then note that MoveTo() is used for humanoids, and not regular models. Use PivotTo() instead.

4 Likes

Hey folks, quick update! Found the solution to my script glitch, so I’m all good now. Thanks for the help, but turns out I don’t need it anymore. Bye! (Post will be deleted, FYI!) oh wait i cant delete it ._.