Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Teleporting Character Script with a Limit to where you can teleport?

Hello, I have made a teleporting script where when you press a button, your Character teleports to where your mouse is located.

My question is, how do I make a limit to where I'm teleporting? For example, you can only teleport a couple of studs from your position before it stops working.

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

function teleport(key)
	if key == "f" then
		player.Character.Torso.CFrame = mouse.Hit
	end
end
mouse.KeyDown:connect(teleport)

1 answer

✓ Accepted answer

Magnitude

There's a special operation you can preform on each Vector3 value, which is called "magnitude" (or, "Magnitude"). Magnitude can give you the distance between two objects by subtracting their positions, and using this magnitude operation on the result. For example:

local Vector1 = Vector3.new(0,50,45)
local Vector2 = Vector3.new(90,87,-10)

-- It's very important you enclose this vector operation in parenthesis, since the order of operations still applies, and we don't want to subtract Vector1 by Vector2.Magnitude.
print((Vector1-Vector2).Magnitude)

The code above will display the distance between these two 3D coordinates, which we can utilize in your situation to create a logic gate using an if statement. Here's how we could check the distance:

local Vector1 = Vector3.new(0,50,45)
local Vector2 = Vector3.new(90,87,-10)

local Range = 10 -- The max range
local Distance = (Vector1-Vector2).Magnitude -- The distance

if Distance > Range then -- If Distance is larger than the "Range" constant, then it will print "Out of range"
	print("Out of range")
else -- But if it's not out of range, than it's obviously in range, so let's print "In range"
	print("In range")
end

Similarly with your situation, we'd apply the same logic. I'd suggest studying this solution to your question, to understand the use of magnitude a lot better:

local Player = game:GetService("Players").LocalPlayer
-- Wait for the Character and it's Torso
local Character = Player.CharacterAdded:wait()
local Torso = Character:WaitForChild("Torso")
local Mouse = Player:GetMouse()

local TeleportRange = 50 -- Max range you can teleport in

-- We can directly connect an anonymous function to this event, since we don't need to reference it again.
Mouse.KeyDown:connect(function(Key)
	if Key:lower() == "f" then
		local Distance = (Mouse.Hit.p-Torso.Position).Magnitude

		-- If the distance is less than the TeleportRange, then allow then teleport.
		if Distance < TeleportRange then
			Torso.CFrame = CFrame.new(Mouse.Hit.p)
		else -- If it's not, print "Too far"
			print("Too far")
		end
	end
end)

Suggestions

I also suggest studying how UserInputService works, since it's very fun to work with and is the preferred method of detecting user input over the PlayerMouse object: http://wiki.roblox.com/index.php?title=API:Class/UserInputService

And that's about it. Hope this helped, let me know if you have any questions.

1 comment

0

Thanks for helping! It works like a charm.

This discussion is preserved as it was. New answers, comments, and votes are closed.