When the "if" value isn't true then the script stops working

So, i’m trying to do this teleport script that has a determined range to where you can teleport to, but when you put your mouse far away and execute the teleport script it stops working, you wait the cooldown but it still doesn’t work, any help?

local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()

local rp = game:GetService("ReplicatedStorage")
local TimeSkip = rp:WaitForChild("TheWorldRemotes"):WaitForChild("TimeSkip")

local UIS = game:GetService("UserInputService")

local debounce = false
local cooldown = 1



UIS.InputBegan:Connect(function(input,isTyping)
	if isTyping then
		return
	elseif input.KeyCode == Enum.KeyCode.X then
		if workspace:FindFirstChild(Player.Name.." Stand") then
					if debounce == false then
		               debounce = true
		
TimeSkip:FireServer()
if Player:DistanceFromCharacter(Mouse.Hit.p) <= 75 then
Player.Character:MoveTo(Mouse.Hit.p)


end		
		    end
		end
	end
end)
TimeSkip.OnClientEvent:Connect(function()
	wait(cooldown)
	debounce = false
end)

For example if you put your cursor over the range of 75 studs, it wont teleport you and even if you wait out the cooldown, it will not work again.

First please indent your code and put it in this form, doing that makes it a lot easier to read and more professional:

Code I Have For You

Second why do you fire “TimeSkip” to the server and then back if you change the debounce in the same script?

1 Like

i don’t get at what you’re trying to get at, could you be a little little bit more specific and explain to me like you are explaining it to a person with half a brain

Indenting is a way to make code look more readable.
Typically to indent you press the tab button, and when you want to de-indent you do shift + tab.`

Here are two code examples.

This is much easier to read, nice and clean.

if SomeVariable then
	if SomeRandomVariable then
		if SomeEvenMoreRandomVariable then
			RandomAction()
			Really1010101111110RandomAction()
			ReallyReallyReallyActionRandom()
		end
	end
end

Notice how this is harder to read than the first example? This example has no indenting.

if SomeVariable then
if SomeRandomVariable then
if SomeEvenMoreRandomVariable then
RandomAction()
Really1010101111110RandomAction()
ReallyReallyReallyActionRandom()
end
end
end

What indenting itself really is, is something I cannot explain because I’m bad at explaining things, so I’ll just grab this from a Wikipedia article

Indentation or indenting may refer to: Indentation (typesetting), the placement of text farther to the right, or left, to separate it from surrounding text. Indentation style, in programming a convention governing the indentation of blocks of code to convey the program’s structure.

The Wikipedia Article

Now to the original post’s question.

The only reason I can get top of my head right now why it wouldn’t be working is, that when you fire TimeSkip it never actually fires back, so Debounce never gets set to false again. An easy fix would be just to remove the TimeSkip.OnClientEvent and then below

if Player:DistanceFromCharacter(Mouse.Hit.p) <= 75 then
...
end

adding this piece of code

wait(Cooldown)
debounce = false

i structured it like this:

local Player = game:GetService(“Players”).LocalPlayer
local Mouse = Player:GetMouse()

local rp = game:GetService(“ReplicatedStorage”)
local TimeSkip = rp:WaitForChild(“TheWorldRemotes”):WaitForChild(“TimeSkip”)

local UIS = game:GetService(“UserInputService”)

local debounce = false
local cooldown = 1

UIS.InputBegan:Connect(function(input,isTyping)
if isTyping then
return
elseif input.KeyCode == Enum.KeyCode.X then
if workspace:FindFirstChild(Player.Name…" Stand") then
if debounce == false then
debounce = true

TimeSkip:FireServer()

      end
  end

end
end)
wait(cooldown)
debounce = false
if Player:DistanceFromCharacter(Mouse.Hit.p) <= 100 then
Player.Character:MoveTo(Mouse.Hit.p)
end

now it doesn’t work after firing it once and it doesn’t teleport the player, i feel like at this point im asking you to do the job but can you point out exactly what i did wrong and how to fix it?

nevermind, i managed to fix it so i’ll just mark your answer as the solution