Boolean Value working only the first time, and after it does not works anymore

So I am making an Undertale game and I was creating an ability/tool named “Teleport”.
I wanted to make a limit and if the player is too far, the cooldown is cleared and the game let player trying another teleportation.
I am using an Boolean Value who changes when its failing, and this one has a problem : it changes but does not block the “if/then” :

Local script :

local RS = game:GetService("ReplicatedStorage")

local cooldownvalue = 3
local teleportevent = RS:WaitForChild("TeleportEvent")
local tool = script.Parent
tool.RequiresHandle = false

function onActivation()
	local player = game.Players.LocalPlayer
	local mouse = player:GetMouse()
	local position = mouse.Hit
	teleportevent:FireServer(position)
	script.Enabled = false
	wait(0.25)
	if script.Parent.Failure.Value == true then
		tool.Name = "Fail!"
		wait(0.5)
		tool.Name = "Teleport"
		script.Enabled = true
	else
		cooldown()
		script.Enabled = true
	end
	script.Parent.Failure.Value = false
end

tool.Activated:Connect(onActivation)
function cooldown()
	tool.Name = "8"
	wait(0.1)
	tool.Name = "7.9"
	wait(0.1)
	tool.Name = "7.8"
	wait(0.1)
	tool.Name = "7.7"
	wait(0.1)
	tool.Name = "7.6"
	wait(0.1)
	tool.Name = "7.5"
	wait(0.1)
	tool.Name = "7.4"
	wait(0.1)
	tool.Name = "7.3"
	wait(0.1)
	tool.Name = "7.2"
	wait(0.1)
	tool.Name = "7.1"
	wait(0.1)
	tool.Name = "7"
	wait(0.1)
	tool.Name = "6.9"
	wait(0.1)
	tool.Name = "6.8"
	wait(0.1)
	tool.Name = "6.7"
	wait(0.1)
	tool.Name = "6.6"
	wait(0.1)
	tool.Name = "6.5"
	wait(0.1)
	tool.Name = "6.4"
	wait(0.1)
	tool.Name = "6.3"
	wait(0.1)
	tool.Name = "6.2"
	wait(0.1)
	tool.Name = "6.1"
	wait(0.1)
	tool.Name = "6"
	wait(0.1)
	tool.Name = "5.9"
	wait(0.1)
	tool.Name = "5.8"
	wait(0.1)
	tool.Name = "5.7"
	wait(0.1)
	tool.Name = "5.6"
	wait(0.1)
	tool.Name = "5.5"
	wait(0.1)
	tool.Name = "5.4"
	wait(0.1)
	tool.Name = "5.3"
	wait(0.1)
	tool.Name = "5.2"
	wait(0.1)
	tool.Name = "5.1"
	wait(0.1)
	tool.Name = "5"
	wait(0.1)
	tool.Name = "4.9"
	wait(0.1)
	tool.Name = "4.8"
	wait(0.1)
	tool.Name = "4.7"
	wait(0.1)
	tool.Name = "4.6"
	wait(0.1)
	tool.Name = "4.5"
	wait(0.1)
	tool.Name = "4.4"
	wait(0.1)
	tool.Name = "4.3"
	wait(0.1)
	tool.Name = "4.2"
	wait(0.1)
	tool.Name = "4.1"
	wait(0.1)
	tool.Name = "4"
	wait(0.1)
	tool.Name = "3.9"
	wait(0.1)
	tool.Name = "3.8"
	wait(0.1)
	tool.Name = "3.7"
	wait(0.1)
	tool.Name = "3.6"
	wait(0.1)
	tool.Name = "3.5"
	wait(0.1)
	tool.Name = "3.4"
	wait(0.1)
	tool.Name = "3.3"
	wait(0.1)
	tool.Name = "3.2"
	wait(0.1)
	tool.Name = "3.1"
	wait(0.1)
	tool.Name = "3"
	wait(0.1)
	tool.Name = "2.9"
	wait(0.1)
	tool.Name = "2.8"
	wait(0.1)
	tool.Name = "2.7"
	wait(0.1)
	tool.Name = "2.6"
	wait(0.1)
	tool.Name = "2.5"
	wait(0.1)
	tool.Name = "2.4"
	wait(0.1)
	tool.Name = "2.3"
	wait(0.1)
	tool.Name = "2.2"
	wait(0.1)
	tool.Name = "2.1"
	wait(0.1)
	tool.Name = "2"
	wait(0.1)
	tool.Name = "1.9"
	wait(0.1)
	tool.Name = "1.8"
	wait(0.1)
	tool.Name = "1.7"
	wait(0.1)
	tool.Name = "1.6"
	wait(0.1)
	tool.Name = "1.5"
	wait(0.1)
	tool.Name = "1.4"
	wait(0.1)
	tool.Name = "1.3"
	wait(0.1)
	tool.Name = "1.2"
	wait(0.1)
	tool.Name = "1.1"
	wait(0.1)
	tool.Name = "1.0"
	wait(0.1)
	tool.Name = "0.9"
	wait(0.1)
	tool.Name = "0.8"
	wait(0.1)
	tool.Name = "0.7"
	wait(0.1)
	tool.Name = "0.6"
	wait(0.1)
	tool.Name = "0.5"
	wait(0.1)
	tool.Name = "0.4"
	wait(0.1)
	tool.Name = "0.3"
	wait(0.1)
	tool.Name = "0.2"
	wait(0.1)
	tool.Name = "0.1"
	wait(0.1)
	tool.Name = "0"
	wait(0.1)
	tool.Name = "Teleport"
	wait(0.1)
end

Server Script :

local RS = game:GetService("ReplicatedStorage")

local TeleportEvent = RS:WaitForChild("TeleportEvent")

TeleportEvent.OnServerEvent:Connect(function(player, position)
	local character = player.Character
	local primarypart = character.PrimaryPart
	if (primarypart.Position - position.Position).Magnitude <= 50 then
		print("good")
		primarypart.CFrame = CFrame.new(position.X, position.Y + 2, position.Z)
	else
		print("fail")
		script.Parent.Failure.Value = true
		print("changed")
		print(script.Parent.Failure.Value)
	end
end)

And some screenshots of the “prints” :
image
(there is writen “true” for the last print but it is not blocking the local script at line 15)

Sorry if its bad, its my first post so i dont know really how to use it ^^
Thanks for everyone who will help me :smiley:

2 Likes

Hello my dear friends I commend you for your effort in writing the cooldown script. I rewrote some of your code. If you want to get returned a value for a RemoteEvent try using Remote Functions they allow callback so you can make a variable instead of using an Instance. (Best Practice)

Remote Functions

local RS = game:GetService("ReplicatedStorage")
local cooldownvalue = 3
local Debounce = false
local teleportevent = RS:WaitForChild("TeleportEvent")
local tool = script.Parent
tool.RequiresHandle = false
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()


function onActivation()
if Debounce == false then
local position = mouse.Hit
teleportevent:FireServer(position)
Debounce = true
	if script.Parent.Failure.Value == true then
		tool.Name = "Fail!"
		wait(0.5)
		tool.Name = "Teleport"
               Debounce = true
	else
		cooldown()
                Debounce = false
       end
wait(cooldownvalue) -- The time it takes for the ability to be fired again
Debounce = false
	script.Parent.Failure.Value = false
end
end


function cooldown()
local i = 8
	for i = 80, 1 do
        i = 8 - i/10 
       tool.Name = i
       task.wait(.1)
end
tool.Name = "Teleport"
end

tool.Activated:Connect(onActivation)

1 Like

thank you for all your work :smiley: ! there is just something I cant fix because I am new to roblox programmation : line 40 (for i, 80, 1 do) there is an error :

Sorry was a mistake on my part, it’s for i = 80, 1 do not, i, 80. This does an iteration of 80 loops for 1 increment hence the 80,1.

It works! but there is one last thing : there is no cooldown…? i think something failed just there :
image
why its blue? ik that says something bad but you know im new so please help another time :slight_smile:

1 Like

So sorry it’s suppose to be for i = 0, 80, 1 do. Awkwarrddd :laughing: 0 is starting value, 80 is end value, 1 is increment value.

1 Like

Ummm, i have really bad news.

  • The cooldown is working… but sometimes its written “7.555556” or something like that :
    image
  • I have the same glitch i got on my script : cooldown when failing teleport…

Video for proofs :

Hello!
So i rewrote the code.

Here it is:

local RS = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")

--// Configuration
local Cooldown = 3 --> Change cooldown here.
local MaxDistance = 50 --> Change the maximum teleport distance here.



local Debounce = false
local Event = RS:WaitForChild("TeleportEvent")
local Tool = script.Parent
Tool.RequiresHandle = false

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local RootPart = Character:WaitForChild("HumanoidRootPart")

local function GetMousePosition()
	local Camera = workspace.CurrentCamera
	local Parameters = RaycastParams.new()
	Parameters.RespectCanCollide = true
	
	local MouseLocationAt2D = UIS:GetMouseLocation()
	local Cast = Camera:ViewportPointToRay(MouseLocationAt2D.X, MouseLocationAt2D.Y)
	local Result = workspace:Raycast(Cast.Origin, Cast.Direction * 5000, Parameters)

	if Result == nil then
		return Cast.Origin + Cast.Direction * 5000
	else
		return Result.Position, Result.Instance
	end
end

local function StartCooldown()
	for i = 0, Cooldown, 0.1 do
		local String = tostring(Cooldown - i):sub(1, 3)
		Tool.Name = String
		
		task.wait(0.1)
	end
	
	Tool.Name = "Teleport"
end

function onActivation()
	if Debounce == false then
		Debounce = true
		
		local Position = GetMousePosition()
		if (RootPart.Position - Position).Magnitude > 50 then
			Tool.Name = "Fail!"
			task.wait(0.5)
			Tool.Name = "Teleport"
			
			Debounce = false
			return
		end
		
		Event:FireServer(RootPart, Position)
		StartCooldown()
		
		Debounce = false
	end
end


Tool.Activated:Connect(onActivation)

And this is the server-side:

local RS = game:GetService("ReplicatedStorage")
local Event = RS:WaitForChild("TeleportEvent")


Event.OnServerEvent:Connect(function(Player, RootPart, Position)
	RootPart.CFrame = CFrame.new(Position)
end)
1 Like

Yipee :smiley: its working (dont understand somethings but no problem)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.