Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I add a specific range and cooldown to a teleportation script?

Basically, I found a script from a question about "Teleporting to the cursor when you press" that fitted what I was making. Heres what i copied.

local Player = game.Players.LocalPlayer 
local Character = Player.Character or Player.CharacterAdded:Wait() 
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart") 
local UserInputService = game:GetService("UserInputService")
local Mouse = Player:GetMouse()


UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end 
	if input.KeyCode == Enum.KeyCode.V and Mouse.Hit then 
		local MouseCFrame = Mouse.Hit  
		HumanoidRootPart.CFrame = MouseCFrame 
	end
end)

I put it in StarterPlayerScripts. I'm just wondering if there is a way to put a cooldown and/or specific range to this teleportation script?

2 comments

0

Do you need cool down OR range, or both of them? I could do the cooldown part. But not sure about the range.

0

i need both. but you can give the cooldown to me if you want, because i need both

1 answer

You can distance check with magnitude, just subtract the two points to find the distance. You can implement a cool down by checking the last time you did something with tick().

local Player = game.Players.LocalPlayer 
local Character = Player.Character or Player.CharacterAdded:Wait() 
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart") 
local UserInputService = game:GetService("UserInputService")
local Mouse = Player:GetMouse()
local LastTeleported = 1


local CoolDown = 1
local Reach = 30

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end 
	if input.KeyCode == Enum.KeyCode.V and Mouse.Target then 
		local Pos = Mouse.Hit.p
		local Distance = (Pos - HumanoidRootPart.Position).magnitude
		if Distance <= Reach and tick() - LastTeleported >= CoolDown then 
			LastTeleported = tick()
			Character:MoveTo ( Pos )
		end
	end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.