How do I add teleportation distance limit to this script?
local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()
local Tool = script.Parent
local debounce = false
Tool.Activated:Connect(function()
if debounce == false then
debounce = true
Player.Character.HumanoidRootPart.CFrame = CFrame.new(Mouse.Hit.p) + Vector3.new(0,1,0)
wait(1)
debounce = false
end
end)
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local tool = script.Parent
local maxDistance = 50 -- the distance from player to mouse pos they max can tp to
local debounce = false
-- making seperate function for efficiency if u add more code in future + helps readability
local function onActivated()
local char = player.Character
local HRP: Part = char.HumanoidRootPart
if not debounce and char then
local mouseHitP = mouse.Hit.p
local distanceBetweenCharAndPart = (mouseHitP - HRP.Position).Magnitude -- distance
if distanceBetweenCharAndPart <= 50 then -- if under or equal distance tp player
debounce = true
-- teleport player to mousehit
HRP.CFrame = CFrame.new(mouseHitP) + Vector3.new(0,3,0)
task.wait(1)
debounce = false
end
end
end
tool.Activated:Connect(onActivated)
This you can do with the script. You set a maxDistance and then the player can teleport to the max distance but not further than that. If you want them to teleport even further you can up the value.
local BWorkpace = game:GetService("Workspace")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
local Character = Player.Character or Player.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local TeleportingTool = script.Parent.Parent -- You need to change the variable
local MaximumDistance = 50
local Debounce = false
TeleportingTool.Activated:Connect(function()
local StartingPosition = TeleportingTool.Handle.CFrame.p
local EndingPosition = Mouse.Hit.Position
local DistanceToTarget = (EndingPosition - StartingPosition).Unit * 250
local RaycastResult = BWorkpace:Raycast(StartingPosition, DistanceToTarget)
if RaycastResult.Instance and RaycastResult.Position then
if RaycastResult.Distance < MaximumDistance then -- You need to change the distance to teleport to the target
if not Debounce then
Debounce = not Debounce
HumanoidRootPart.CFrame = CFrame.new(Mouse.Hit.p)
task.wait(1)
Debounce = not Debounce
end
else
if not Debounce then
Debounce = not Debounce
local TargetPosition = StartingPosition + (EndingPosition - StartingPosition).Unit * MaximumDistance
HumanoidRootPart.CFrame = CFrame.new(TargetPosition) + Vector3.new(0, 1, 0)
task.wait(1)
Debounce = not Debounce
end
end
end
end)
local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()
local Tool = script.Parent
local debounce = false
local teleportLimit = 50
Tool.Activated:Connect(function()
if not debounce then
debounce = true
local playerPosition = Player.Character.HumanoidRootPart.Position
local mousePosition = Mouse.Hit.Position
local direction = (mousePosition - playerPosition).Unit
local distance = (playerPosition - mousePosition).Magnitude
if distance > teleportLimit then
distance = teleportLimit
end
local newPosition = playerPosition + direction * distance
Player.Character.HumanoidRootPart.CFrame = CFrame.new(newPosition + Vector3.new(0, 1, 0))
task.wait(1)
debounce = false
end
end)