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

[SOLVED] How to do a tool that only deals damage when activated?

Asked by 6 years ago
Edited 6 years ago

Hey, I was trying to do a tool that deals damage when touching a part while the tool is activated. My problem is that it deals damage (currently prints something) whenever the part is touched, regardless the weapon is activated or not. Here's the piece of the script. (It is a localscript inside a tool on player's backpack)

----//RightArm Damaging Function\\----

function RightDamage() --fired when tool is activated
    if LastArm == "Left" then --checks if the moving arm is the right
        char.RightHand.Touched:Connect(function(RDamager)
            if not damagedebouce then
                damagedebouce = true
                print("Right Punch Hit") --I will change this with the damage
                wait(0.6)
                damagedebouce = false
            end
        end)
    end
end

Here's the full scripts so you can see the local values and other things


----//Setting Arm Alternator\\---- LastArm = "Left" ----//Setting Debounces\\---- debounce = false damagedebouce = false ----//Setting Local Values\\---- local Tool = script.Parent local LeftPunch = Tool:WaitForChild("LeftPunch") local RightPunch = Tool:WaitForChild("RightPunch") local plr = game.Players.LocalPlayer local char = plr.Character or plr.CharacterAdded:wait() while char.Parent == nil do char.AncestryChanged:wait() end local humanoid = char:WaitForChild("Humanoid") ----//Loading Animations\\---- local AnimTrack = humanoid:LoadAnimation(RightPunch) local AnimTrack2 = humanoid:LoadAnimation(LeftPunch) ----//Animate Function\\---- function Animator() if not debounce then debounce = true if LastArm == "Left" then AnimTrack:Play() LastArm = "Right" wait(0.5) debounce = false elseif LastArm == "Right" then AnimTrack2:Play() LastArm = "Left" wait(0.5) debounce = false end end end ----//RightArm Damaging Function\\---- function RightDamage() if LastArm == "Left" then char.RightHand.Touched:Connect(function(RDamager) if not damagedebouce then damagedebouce = true print("Right Punch Hit") wait(0.6) damagedebouce = false end end) end end ----//Calling Functions\\---- Tool.Activated:Connect(Animator) Tool.Activated:Connect(RightDamage)

Thanks for your attention.

0
You shouldn't have the Touched event inside the Activated event. If it is spammed, it will create tons of connections, and those things pile up you know. User#19524 175 — 6y
0
how do I do it then? VewixxPlayer 67 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

I solved it myself. I added a value called isActive and set it to true when the Tool was activated, and set to false when the punch animation ended (0.25 seconds), so I was able to check if the player was punching using that value. I will put the code here so if someone has the same problem they can solve it, too.

----//Setting Active Checker\\----

isActive = false

^That creates the value^

----//Animate Function\\----

function Animator()
    if not debounce then
        debounce = true
        isActive = true --this verifies that tool is activated
        if LastArm == "Left" then
            AnimTrack:Play()
            LastArm = "Right"
            wait(0.25) --wait animation lenght so it deactivates when animation ends
            isActive = false --this verifies that tool is no longer active
            wait(0.25)
            debounce = false
        elseif LastArm == "Right" then
            AnimTrack2:Play()
            LastArm = "Left"
            wait(0.25) --wait animation lenght so it deactivates when animation ends
            isActive = false --this verifies that tool is no longer active
            wait(0.25)
            debounce = false
        end
    end
end

^That sets the value to true when Tool is activated and to false when is deactivated^

----//RightArm Damaging Function\\----

function RightDamage() 
    char.RightHand.Touched:Connect(function(hit)
        if not damagedebouce then 
            if isActive == true then --checks if tool is active
                if LastArm == "Right" then
                    damagedebouce = true
                    print("Right Punch Hit")
                    wait(0.5)
                    damagedebouce = false
                end
            end
        end
    end)
end

^And that checks if the tool is active so it only deals damage (currently printing) while the Animation is playing^

0
Alternatively, you could use Equipped if you want it when it's equipped. chexburger 358 — 6y
0
It is a punch tool script and I wanted it to make damage while the player was punching, do I did it using the animation times to check if player was punching VewixxPlayer 67 — 6y
Ad

Answer this question