How to add distance limit to this teleportation tool?

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)
1 Like

Hey there, something like this :smiley:

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)

its working but how do i make that player will still teleport on max distance even when you clicked on long distance?

I don’t think I understand your question, could you explain further?

I explain, when you click off maximum radius, you will teleport at maximum radius

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.

You see right now it’s at 50 studs I’m guessing.

No I meant, the script need to teleport you to maximum distance even when you click farther maximum distance

in current script, its doesnt teleport you when the mouse is not in the maximum distance

Okay, so you don’t want any limit? That’s kind of going against your title :smiley:

its should teleport you in maximum distance when your mouse is beyond the maximum distance

Oh you mean when the mouse is further than the max distance then it still teleports player but just to maxDistance which would be 50 for example?

Yes, i meant that, thanks for understanding

So, any ideas about that, cuz I tried to do it and nothing works?

Greetings,

I’ve written a code for you.

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)

I hope it will work, have a nice day.


It’s giving me this error when i using tool

Raycast does not detect an Instance, I mean an object. Try to click a 3D object, not the space.

im clicking it, and it should not needed to click the object.
This script working very weird not gonna lie.

You are overcomplicating it.

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)
1 Like

I agree with you, I apologize.

It’s all good, Mistakes make you stronger :muscle:

1 Like