Script Still teleports even player even when they do not have the part equipped

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)

thanks in advance, snipperdiaper

2 Likes

Try changing “local debounce = true” to “debounce = true”. I’m pretty sure it would work.

2 Likes

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.

Thank you so much, you are a saint.

I appreciate how much effort you went through to fix this.

1 Like

instead of doing this, use a for loop For Loops(api reference) and in case you’re confused, watch this video from beginning to end For Loops - AlvinBlox - YouTube

player.PlayerGui.ClickToTeleportGui.TextLabel.Text = “CoolDown: 3”
wait(1)
player.PlayerGui.ClickToTeleportGui.TextLabel.Text = “CoolDown: 2”

I made the script for ya, but dont forget to see the documentary or the video about for loop

for i = 5, 0, -1 do
player.PlayerGui.ClickToTeleportGui.TextLabel.Text = "CoolDown: "…i
wait(1)
end

1 Like