Edit: grammar and code copy/pasteability, code snippet works when applied in workspace without duplicating. Origin code is too large to paste and describe quickly, will update upon request
Hey there, still semi-new to scripting and I appear to be the victim of bad writing, though I’m not sure how to fix the practice.
When I call a function when criteria is met, the function fires an additional time every time it’s called. Unsure if this qualifies as a data leak as I’m not well versed in the coding language yet but it’s causing me issues, here’s an abstract example of the code:
function Function()
if criteria == true then
print("Printed")
else
return
end
end
ActivationRequirementMet:Connect(Function)
Now every time I meet the activation requirement, the function fires multiple times, for example:
Printed
Printed(x2)
Printed(x3)
etc...
To move from the abstract to my specific issue in the matter, here is the least complex example I have:
local Enabled = true
local Combo = 1
local Mouse = game.Players.LocalPlayer:GetMouse() -- its a localscript
function canHit()
if Enabled == true and
1 == 1 --other conditions are here, removed for reading ease
return true
else
return false
end
end
function basicAttack()
if Tool.Parent ~= Character then return end --delete if testing, though it may be the cause of the issue
if canHit() == true then
if Combo == 1 then
Enabled = false
anim1:Play() -- anim is specified in locals above, delete if you test in your workspace
print("hitstart")
anim1:GetMarkerReachedSignal("hitEnds"):Connect(function() -- replace with wait() if you test in workspace
Enabled = true
end)
end
else
print("Attack Denied.")
return
end
end
Mouse.Button1Down:Connect(basicAttack)
The output; note the “block” is used to disrupt the output so that the repeating effect is shown. Every instance of “hitstart” is a click.
23:10:30.111 hitstart - Client - Main:102
23:10:32.927 block - Client - Main:160
23:10:33.862 ▶ hitstart (x2) - Client - Main:102
23:10:35.494 block - Client - Main:160
23:10:36.529 ▶ hitstart (x3) - Client - Main:102
23:10:38.211 block - Client - Main:160
23:10:38.944 ▶ hitstart (x4) - Client - Main:102
I feel that this is a fundamental topic, somehow I’ve missed it throughout my readings within lua, any references to reading material would be appreciated. I’ve searched the devhub and the (gasp) third page of google and havent found anything similar to this issue, though I may be searching wrong.
Thanks in advance. If you’d like to reply with a specific code, keeping it abstract would be preferred.