Is there a more simple way to make a random teleport?

Hello there! Currently making a small obby to practice scripting!

So my goal here is to make a portal to where if you enter it (touch it) you will be teleported to a random obstacle and the end portal to that obstacle will (eventually) give you coins. I’m not worried about the coins just yet. But the thing that the players teleport to is just a brick with a specific name. I also am coming across the issue as to when a person is teleported they may get stuck or their character will fall over and I’m not wanting that to happen but it does regardless. CanCollide is off for the teleported brick. My question is if there would be a more simple way, say maybe I could have a brick or portal that’s just simply named “exit” and it does random math to teleport you to one of them instead of what I have now which would take ages to code if I were to have more obstacles, as of now I only have the 3 and this is the code I’m working with. Thank you for your help!

function onTouch(part)

local s=math.random(1,4)

if s == 1 then

part.CFrame = CFrame.new(script.Parent.Parent.mainisland.Position.x, script.Parent.Parent.mainisland.Position.y, script.Parent.Parent.mainisland.Position.z)

elseif s == 2 then

part.CFrame = CFrame.new(script.Parent.Parent.sharkbridges.Position.x, script.Parent.Parent.sharkbridges.Position.y, script.Parent.Parent.sharkbridges.Position.z)

elseif s == 3 then

part.CFrame = CFrame.new(script.Parent.Parent.dungeon1.Position.x, script.Parent.Parent.dungeon1.Position.y, script.Parent.Parent.dungeon1.Position.z)

elseif s == 4 then

end

end

script.Parent.Touched:Connect(onTouch)

5 Likes

here is a improved version of your script and should work:

function onTouch(part)
if game.Players:GetPlayerFromCharacter(part.Parent) then
local s=math.random(1,4)
local hrp = part.Parent.HumanoidRootPart
if s == 1 then
hrp.CFrame = script.Parent.Parent.mainisland.CFrame
elseif s == 2 then
hrp.CFrame = (script.Parent.Parent.sharkbridges.CFrame
elseif s == 3 then
hrp.CFrame = script.Parent.Parent.dungeon1.CFrame
elseif s == 4 then
end
end
end
script.Parent.Touched:Connect(onTouch)
3 Likes

this is the simplest thing I can do

local portalBase = script.Parent.Parent
local targets = {
	portalBase.mainisland,
	portalBase.sharkbridges,
	portalBase.dungeon1,
}

function onTouch(part)
	local s = math.random(1,#targets)
	part.CFrame = CFrame.new(targets[s].Position)
end

script.Parent.Touched:Connect(onTouch)

if the tragets were in a single folder, this would be even simpler

local targetsFolder = script.Parent.Parent.Targets
local targets = targetsFolder:GetChildren()

function onTouch(part)
	local s = math.random(1,#targets)
	part.CFrame = CFrame.new(targets[s].Position)
end

script.Parent.Touched:Connect(onTouch)

don’t forget to use the debounce technique to avoid strange behaviors.

To avoid getting stuck you can use MoveTo

character:MoveTo(targets[s].Position)

or add a displacement vector

part.CFrame = CFrame.new(targets[s].Position + Vector3.new(0, 5, 0))

no need to disable CanCollide

8 Likes

You could use math.random for the random tp.

3 Likes

Wow! You went above and beyond, essentially rewriting the entire script. That seemed to work very well. No more falling when teleporting. And, it’s much easier to have all the targets in the folder. Way more clean and organized. Thanks!

2 Likes