Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago

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?

0
Do you need cool down OR range, or both of them? I could do the cooldown part. But not sure about the range. Techyfied 114 — 4y
0
i need both. but you can give the cooldown to me if you want, because i need both Sikeking66 9 — 4y

1 answer

Log in to vote
0
Answered by
Azarth 3141 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

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)
Ad

Answer this question