local debounce = false
script.Parent.Activated:Connect(function() -- Tool
if not debounce then
debounce = true
print("Debounce")
wait(2)
debounce = false
end
end)
The error is it has no cooldown.
What did i do wrong?
Your debounce looks likes its working fine. Just make sure that whatever code you intend to run comes immediately after setting the debounce variable to true:
script.Parent.Activated:Connect(function() -- Tool
if not debounce then
debounce = true
--Run whatever code you need to here.
--If its a function that will yield you may need to use coroutines.
--(I can help you with that if you need it)
wait(2)
debounce = false
end
end)
local Player = game:GetService('Players').LocalPlayer
local Mouse = Player:GetMouse()
local debounce = false
script.Parent.Activated:Connect(function()
if not debounce then
debounce = true
local character = script.Parent.Parent
game:GetService('UserInputService').InputBegan:Connect(function(i,o)
if o then return end
if i.UserInputType == Enum.UserInputType.MouseButton1 then
local MousePos = Mouse.Hit
game.ReplicatedStorage.Events.Teleport:FireServer(MousePos, character)
script.Parent.Magic:Play()
print("Sent")
wait(2)
debounce = false
end
end)
end
end)
What i want to show with this is the code i’m using
how come i feel like in this situation its just bypassing the if not debounce becuase it exists and isnt nil, if that were true then you would go
local debounce = false
--function thingy
if debounce == false then
debounce = true
--code
wait(2)
debounce = false
end
edit:
if you were to look at it not is checking if its the oppsotie of the condition,
because if instance/value then checks if it exists
that would mean that if not instance/value then would check if it is nil
local Player = game:GetService('Players').LocalPlayer
local Mouse = Player:GetMouse()
local debounce = false
script.Parent.Activated:Connect(function()
if not debounce then
debounce = true
local character = script.Parent.Parent
local MousePos = Mouse.Hit
game.ReplicatedStorage.Events.Teleport:FireServer(MousePos, character)
script.Parent.Magic:Play()
print("Sent")
wait(2)
debounce = false
end
end)
Your issue in the previous code is you’re setting up a new event listener every time the Tool is activated. If you intend on using UserInputService, you can bypass the Activated event of the Tool all together. You will have to add another variable for tracking whether the Tool is equipped though. Here is the solution:
local userInputService = game:GetService("UserInputService")
local player = game:GetService('Players').LocalPlayer
local mouse = player:GetMouse()
local debounce = false
local equipped = false
script.Parent.Equipped:connect(function()
equipped = true
end)
script.Parent.Unequipped:connect(function()
equipped = false
end)
userInputService.InputBegan:connect(function(io, gp)
if equipped and not gp then --First check to ensure the tool is equipped and the input is not processed by the game already
if io.UserInputType == Enum.UserInputType.MouseButton1 then --Check for the input you want
if not debounce then --Then debounce
debounce = true
--Do what you want to do
local character = player.Character
local mousePos = mouse.Hit
game.ReplicatedStorage.Events.Teleport:FireServer(mousePos, character)
script.Parent.Magic:Play()
print("Sent")
--Wait for debounce cooldown
wait(2)
debounce = false
end
end
end
end)
this would be my solution including using your code, its different than @East98 that is right above me but it does about the same thing.
local Player = game:GetService('Players').LocalPlayer
local Mouse = Player:GetMouse()
local debounce = false
local ImputService = game:GetService("UserImputService")
script.Parent.Activated:Connect(function()
if debounce == true then return end
debounce = true
for i = 1, 200,1 do
local character = Player.Character
local keys = ImputService:GetKeysPressed()
for _. imput in pairs(keys) do
if imput.UserInputType == Enum.UserInputType.MouseButton1 then
local MousePos = Mouse.Hit
game.ReplicatedStorage.Events.Teleport:FireServer(MousePos, character)
script.Parent.Magic:Play()
print("Sent")
end
end
wait(.01)
end
debounce = true
end)
there is probally a better way to time this using something like os.time becuase
this will cause it to not be exactly 2 seconds, looping through all keys pressed
will take time, though very small at that.