01 | local p = game.Players.LocalPlayer |
02 | local char = p.Character |
03 | local root = char:WaitForChild( 'HumanoidRootPart' ) |
04 | local mouse = p:GetMouse() |
05 |
06 | function tele() |
07 | char.Humanoid:MoveTo(mouse.Hit.p) |
08 | end |
09 |
10 | mouse.Button 1 Down:Connect( function () |
11 |
12 | tele() |
13 |
14 | 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?
Check the distance with an if statement. You also have other important problems.
01 | --consider using variable "player" over "p" |
02 | local p = game.Players.LocalPlayer |
03 | local char = p.Character or p.CharacterAdded:wait() --incase char is not loaded |
04 | local root = char:WaitForChild( 'HumanoidRootPart' ) |
05 | local mouse = p:GetMouse() |
06 |
07 | function tele() |
08 | --define vectors |
09 | local origin = root.CFrame.p |
10 | local target = mouse.Hit.p + Vector 3. 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 |
15 | end |
16 |
17 | --since tele is a basic function, you can simply pass it in |
18 | mouse.Button 1 Down:Connect(tele) |
If you don't want to prohibit the teleport, but minimize it, you can shorten the position vector.
01 | function tele() |
02 | local origin = root.CFrame.p |
03 | local target = mouse.Hit.p + Vector 3. 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) |
10 | end |
Edit: It's best to offset the mouse height.