Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can i make all this one move have a debounce but all other's are able to be played?

if you don't get it by the title i want to make a debounce where all other script are able to be press but this still has a debounce. How can i do that here is my script.

local Player = game.Players.LocalPlayer
local char = Player.Character
local en = true
local den = true
local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.Q then
if not den then return end
den = false
local Part = Instance.new("Part")
Part.Parent = workspace
Part.CFrame = char.Head.CFrame * CFrame.new(0,0,-5)
wait()
en = true
wait(3)
den = true
	end
end)

UIS.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.E then
if not en then return end
en = false
local Part2 = Instance.new("Part")
Part2.Parent = workspace
Part2.CFrame = char.Head.CFrame * CFrame.new(0,0,-5)
wait()
den = true
wait(3)
en = true
	end
end)

2 comments

0

You shouldn't create a whole different for a different key code, use 'elseif', because currently you're creating new connections each time a button is pressed.

0

incapaz, how would i use elseif in this script?

1 answer

✓ Accepted answer

The wiki has a really nice article regarding debounces, I recommend giving it a read.

The below will do as I believe you intend. If you use one move, it'll wait until it finishes executing before you're able to use it again. However, the other move will still be available for use.

local Player = game.Players.LocalPlayer
local char = Player.Character or Player.CharacterAdded:Wait();
local UIS = game:GetService("UserInputService")

local attacks = { -- Dictionary mapping relevant keycodes to attacks
	[Enum.KeyCode.Q] = function()
		local Part = Instance.new("Part")
		Part.Parent = workspace
		Part.CFrame = char.Head.CFrame * CFrame.new(0,0,-5)
		wait(3)
	end;
	[Enum.KeyCode.E] = function()
		local Part = Instance.new("Part")
		Part.Parent = workspace
		Part.CFrame = char.Head.CFrame * CFrame.new(0,0,-5)
		wait(3)
	end;
}
local function ForceDebounce(func) -- Forces a debounce onto the function passed in
	local debounce = false;
	return function(...)
		if(not debounce) then
			debounce = true;
			func(...);
			debounce = false;
		end
	end
end
local function ForceDebounceAllAttacks() -- Forces debounce on each function in the dictionary above
	for i, v in pairs(attacks) do
		attacks[i] = ForceDebounce(v);
	end
end

ForceDebounceAllAttacks();
UIS.InputBegan:Connect(function(Input)
	for i, v in pairs(attacks) do -- loop through attacks dictionary
		if(Input.KeyCode == i) then 
			v(); -- Run the function associated with key
		end
	end
end)
  • Note that you'll be able to use both moves simultaneously.

Let me know if you have any questions.

Edit: Added explanation

I added a few comments to make it a bit easier to understand.

The basic idea is that, rather than writing out some long-winded series of if-statements in your InputBegan connection, I just used a table, wherein the indices are keys you can press, and the values are the functions that should execute when those keys are pressed. I then forced a debounce on each of them (using a function that takes advantage of closures). From there, it's a simple matter of running through every attack in the table through a generic for loop, checking against the input, and then executing the attack if the key was pressed.

I hope this helps shed a little more light for what's going on in the code above, I apologize for the added confusion.

1 comment

0

I honesly have no idea whats goin on in this script. Can you explain it more?

This discussion is preserved as it was. New answers, comments, and votes are closed.