Feel free to point out if theres something that I'm just missing here, but I was programming a simple sword script that involves simply clicking the button and dealing damage while playing an animation.
I have a BoolValue called 'CanDamage' that makes sure that the sword doesn't infinitely hit someone, but the If statement in my global script (which checks to make sure that CanDamage is true before triggering the damage numbers) randomly fails after the first hit even though the value (i checked it in the hiearchy) is set to True.
Is there something that I'm missing in my scripts, perhaps a timing problem where it switches off, checks, and then switches back on all in the blink of an eye?
Local Script:
local CanAttack = true -- Theres a difference between CanAttack and CanDamage (one is a variable and the other is a BoolValue) script.Parent.Parent.Activated:Connect(function() local attack = script.Parent.Parent.Parent:FindFirstChild('Humanoid'):LoadAnimation(script.swing) -- (it cuts off right there, its nothing important though ^) -- (its just loading an animation) if (CanAttack) then script.Parent.SwordGlobal.CanDamage.Value = true attack:Play() CanAttack = false wait(0.8) attack:Stop() CanAttack = true script.Parent.SwordGlobal.CanDamage.Value = false end end)
Global Script:
script.Parent.Touched:Connect(function(p) print('Contact') local hum = p.Parent:FindFirstChild('Humanoid') or p.Parent.Parent:FindFirstChild('Humanoid') if (script.CanDamage.Value == true) and (hum) then print('Damage') hum:TakeDamage(20) script.CanDamage.Value = false end end)
(Within the GlobalScript, after printing "Contact" and "Damage" the first time I hit it, it deals damage. The second use of the sword, however, just prints "Contact" which means its getting stuck at:)
if (script.CanDamage.Value == true) and (hum) then
(After checking, its the damagevalue check thats failing here even though the damagevalue is set to true?)
If no one wants to answer such a vague question I honestly don't blame them, its a bit hard to explain in text format.