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

How do i make it so the user can only teleport for a maximum of 100 studs?

Asked by 7 years ago

local p=game.Players.LocalPlayer local char=p.Character local root=char:WaitForChild('HumanoidRootPart') local mouse=p:GetMouse() function tele() char.Humanoid:MoveTo(mouse.Hit.p) end mouse.Button1Down:Connect(function() tele() end)

the script above would allow the player to teleport anywhere on the map without limitations. is there any way for it to not activate if the distance is farther than 100 studs?

0
Humanoid MoveTo makes the character walk to a location, not teleport. 1waffle1 2908 — 7y
0
why don't you just use mouse.Button1Down(tele) abnotaddable 920 — 7y

1 answer

Log in to vote
2
Answered by
cabbler 1942 Moderation Voter
7 years ago
Edited 7 years ago

Check the distance with an if statement. You also have other important problems.

--consider using variable "player" over "p"
local p=game.Players.LocalPlayer
local char=p.Character or p.CharacterAdded:wait() --incase char is not loaded
local root=char:WaitForChild('HumanoidRootPart')
local mouse=p:GetMouse()

function tele()
    --define vectors
    local origin = root.CFrame.p
    local target = mouse.Hit.p + Vector3.new(0,3,0) --elevate root
    if (target-origin).magnitude < 100 then --distance between
        --MoveTo should be used on character, not humanoid
        char:MoveTo(target)
    end
end

--since tele is a basic function, you can simply pass it in
mouse.Button1Down:Connect(tele)

If you don't want to prohibit the teleport, but minimize it, you can shorten the position vector.

function tele()
    local origin = root.CFrame.p
    local target = mouse.Hit.p + Vector3.new(0,3,0)
    local diff = target-origin
    if diff.magnitude > 100 then
        --same direction (unit) but length 100
        target = origin + diff.unit * 100
    end
    char:MoveTo(target)
end

Edit: It's best to offset the mouse height.

Ad

Answer this question