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 8 years ago
01local p=game.Players.LocalPlayer
02    local char=p.Character
03 local root=char:WaitForChild('HumanoidRootPart')
04local mouse=p:GetMouse()
05 
06function tele()
07char.Humanoid:MoveTo(mouse.Hit.p)
08end
09 
10mouse.Button1Down:Connect(function()
11 
12tele()
13 
14end)

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 — 8y
0
why don't you just use mouse.Button1Down(tele) abnotaddable 920 — 8y

1 answer

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

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

01--consider using variable "player" over "p"
02local p=game.Players.LocalPlayer
03local char=p.Character or p.CharacterAdded:wait() --incase char is not loaded
04local root=char:WaitForChild('HumanoidRootPart')
05local mouse=p:GetMouse()
06 
07function tele()
08    --define vectors
09    local origin = root.CFrame.p
10    local target = mouse.Hit.p + Vector3.new(0,3,0) --elevate root
11    if (target-origin).magnitude < 100 then --distance between
12        --MoveTo should be used on character, not humanoid
13        char:MoveTo(target)
14    end
15end
16 
17--since tele is a basic function, you can simply pass it in
18mouse.Button1Down:Connect(tele)

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

01function tele()
02    local origin = root.CFrame.p
03    local target = mouse.Hit.p + Vector3.new(0,3,0)
04    local diff = target-origin
05    if diff.magnitude > 100 then
06        --same direction (unit) but length 100
07        target = origin + diff.unit * 100
08    end
09    char:MoveTo(target)
10end

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

Ad

Answer this question