Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 7 years ago

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

Log in to vote
2
Answered by
funzrey 58
7 years ago

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.

0
Oh of course I remember someone telling something about magnitude but i probably forgot about it,thanks alot funrey. extremeyes 10 — 7y
Ad

Answer this question