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

How do I make this teleport script work if destination is within 50 studs?

Okay all I need to know is I want the player to teleport anywhere but it has to be less than a specific distance, lets say 50 studs how would I add that to my script? What would I use? script ----

p = game.Players.LocalPlayer
c = p.Character
mouse = p:GetMouse()
k = false
dist = 50

function Main()
	if k == false and p:WaitForChild("Data").doingMove.Value == false then
		k = true
		local pos = mouse.Hit.p
		p:WaitForChild("Data").doingMove.Value = true
		c.Torso.CFrame = CFrame.new(Vector3.new(pos.x, pos.y+3, pos.z))
		wait(.5)
		p:WaitForChild("Data").doingMove.Value = false
		wait(2)
		k = false
		end
end


script.Parent.Activated:connect(Main)

1 answer

✓ Accepted answer

So, there's this thing called Magnitude. In vectors (what roblox uses for position) it means length (If you want to study up on the math of magnitude, goto the wiki page: http://wiki.roblox.com/index.php?title=Magnitude ) Magnitude basically gets distance between two vectors in ROBLOX's case.

Here's a clip of code to help you get started:

local mag = (mouse.Hit.p - c.Torso.Position).magnitude
if mag < 50 then
 teleport()
end

Do NOT copy and paste, this is just an example using some of your variables.

1 comment

0

Oh of course I remember someone telling something about magnitude but i probably forgot about it,thanks alot funrey.

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