Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Sword Script damages once and then never again (?)

Asked by 5 years ago

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.

0
This is because it's not replicating what the local script is doing to BoolValue. Your server script sees that Value is still true and then makes it false after damaging and therefore you get you "only damages once" problem. xPolarium 1388 — 5y
0
Look up FilteringEnabled as it's something you need to know for working between client and server. You would need to use a RemoteEvent instead of BoolValues. xPolarium 1388 — 5y
0
Also they're not called "global scripts". User#19524 175 — 5y

Answer this question