I've almost got my desired click/hold fire system for the projectile weapons in my game working as intended, with the intent being that when you click (specifically on release), you launch one attack, but if you hold the button, you instead do another. However, in my testing the click ability activates immediately upon triggering the desired event (primary_ability, in this case) before it works as intended. This results in either the click ability being fired twice in quick succession (once on click, once on release) or the click ability being used right before the hold ability, neither of which is desired.
The weapon I was testing this with launches a fireball as the click attack and acts like a flamethrower for the hold attack. I set up the fireball up in a child weapon parented to the main one, which has the flamethrower. Both abilities have their bindings set to None and are disabled, with them being activated via the Activate() ability function. The flamethrower weapon is set up with burst fire and has Burst Stops on Release set to true.
I've included the code used for this script below:
local equipment = script:FindAncestorByType("Equipment")
local owner = equipment.owner
local weaponFireBinding = script:GetCustomProperty("WeaponFireBinding")
local clickAbility = script:GetCustomProperty("ClickAbility"):WaitForObject()
local holdAbility = script:GetCustomProperty("HoldAbility"):WaitForObject()
local holdDelay = script:GetCustomProperty("HoldDelay")
-- Burst fire is expected with held attacks.
local holdDuration = holdAbility.executePhaseSettings.duration
local lastTime = 0
local isHeld = false
function OnBindingPressed(player, bindingPressed)
if bindingPressed == weaponFireBinding then
lastTime = time()
isHeld = true
-- Wait and check if button is still held.
Task.Wait(holdDelay)
-- If the hold mode is burst fire, activate just fast enough for the bursts.
while isHeld == true do
holdAbility:Activate()
Task.Wait(holdDuration) --Prevents crashes and lag.
end
end
end
function OnBindingReleased(player, bindingReleased)
if bindingReleased == weaponFireBinding then
-- For checking if the button is still being held above.
isHeld = false
-- If the button hasn't been held long enough, consider it a click/press.
if holdDelay >= time() - lastTime then
clickAbility:Activate()
end
end
end
function OnEquipped(weapon, player)
player.bindingPressedEvent:Connect(OnBindingPressed)
player.bindingReleasedEvent:Connect(OnBindingReleased)
end
equipment.equippedEvent:Connect(OnEquipped)
If anyone can help me figure out where that extra ability activation is coming from and how to prevent it, I would greatly appreciate it.