Alright, so in this script, the main goal is to teleport the player to the cursor position when the tool is equipped. However, it still teleports the player even when they do not have it equipped.
And if there already wasnt enough problems, the script also breaks the cool down so when you unequip it you can teleport again,
full script:
local Tool = script.Parent
Tool.Equipped:Connect(function(mouse)
local char = script.Parent
local humanoid = char:FindFirstChild("HumanoidRootPart")
local player = game:GetService("Players").LocalPlayer
local debounce = true
local mouse = player:GetMouse()
mouse.Button1Down:Connect(function()
local position = mouse.Hit.Position
local function FireServer()
game:GetService("ReplicatedStorage").ClickToTeleportEvent:FireServer(position)
end
if debounce == true then
debounce = false
FireServer()
player.PlayerGui.ClickToTeleportGui.TextLabel.TextTransparency = 0
player.PlayerGui.ClickToTeleportGui.TextLabel.BackgroundTransparency = 0
player.PlayerGui.ClickToTeleportGui.TextLabel.Text = "CoolDown: 5"
wait(1)
player.PlayerGui.ClickToTeleportGui.TextLabel.Text = "CoolDown: 4"
wait(1)
player.PlayerGui.ClickToTeleportGui.TextLabel.Text = "CoolDown: 3"
wait(1)
player.PlayerGui.ClickToTeleportGui.TextLabel.Text = "CoolDown: 2"
wait(1)
player.PlayerGui.ClickToTeleportGui.TextLabel.Text = "CoolDown: 1"
wait(1)
player.PlayerGui.ClickToTeleportGui.TextLabel.Text = "CoolDown: 0"
wait(1)
player.PlayerGui.ClickToTeleportGui.TextLabel.TextTransparency = 1
player.PlayerGui.ClickToTeleportGui.TextLabel.BackgroundTransparency = 1
player.PlayerGui.ClickToTeleportGui.TextLabel.Text = "CoolDown: 2"
debounce = true
end
end)
end)
Yeah because you have to check IF the tool is equipped, which is what you aren’t doing here. Also, everytime you equip the tool, a new function linked to Button1Down event is made. You can fix it just by removing the Tool.Equipped function and leaving the Button1Down event only, but also adding an if statement that checks if the tool is parented to the character.
Correct code [use if you can’t do it on your own]:
local Tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:FindFirstChild("HumanoidRootPart")
local debounce = true
local mouse = player:GetMouse()
mouse.Button1Down:Connect(function()
if tool.Parent ~= char then
return
end
local position = mouse.Hit.Position
local function FireServer()
game:GetService("ReplicatedStorage").ClickToTeleportEvent:FireServer(position)
end
if debounce == true then
debounce = false
FireServer()
player.PlayerGui.ClickToTeleportGui.TextLabel.TextTransparency = 0
player.PlayerGui.ClickToTeleportGui.TextLabel.BackgroundTransparency = 0
player.PlayerGui.ClickToTeleportGui.TextLabel.Text = "CoolDown: 5"
task.wait(1)
player.PlayerGui.ClickToTeleportGui.TextLabel.Text = "CoolDown: 4"
task.wait(1)
player.PlayerGui.ClickToTeleportGui.TextLabel.Text = "CoolDown: 3"
task.wait(1)
player.PlayerGui.ClickToTeleportGui.TextLabel.Text = "CoolDown: 2"
task.wait(1)
player.PlayerGui.ClickToTeleportGui.TextLabel.Text = "CoolDown: 1"
task.wait(1)
player.PlayerGui.ClickToTeleportGui.TextLabel.Text = "CoolDown: 0"
task.wait(1)
player.PlayerGui.ClickToTeleportGui.TextLabel.TextTransparency = 1
player.PlayerGui.ClickToTeleportGui.TextLabel.BackgroundTransparency = 1
player.PlayerGui.ClickToTeleportGui.TextLabel.Text = "CoolDown: 2"
debounce = true
end
end)
I also updated your wait() (now deprecated) to the new task library, which is task.wait() instead.
EDIT: I also suggest to update mouse.Button1Down (as it’s deprecated) to UserInputType.MouseButton1 through UserInputService, but it’s your choice.