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

How do i fix my punch script?

Asked by 3 years ago
Edited 3 years ago

Theres a few problems with my script 1. The animation is spammable 2. the player does damage just by walking up to a player when the right hand touches. But i want it to only do damage when the punch animation is played

canDoDmg = true
dmg = 25

local Plr = game.Players.LocalPlayer
local UserInputService = game:GetService("UserInputService")
local Char = Plr.Character or Plr.CharacterAdded:Wait()
local Humanoid = Char:WaitForChild("Humanoid")
local Anim = Instance.new("Animation")
Anim.AnimationId = "rbxassetid://6203475027"
local track = Humanoid:LoadAnimation(Anim)

UserInputService.InputBegan:Connect(function(InputObject)
    if InputObject.UserInputType == Enum.UserInputType.MouseButton1 then
        track:Play()
        script.Parent.RightHand.Touched:connect(function(hit)
            if hit.Parent.Humanoid and canDoDmg == true then
               hit.Parent.Humanoid:TakeDamage(dmg)
            end
        end)
        wait(10)
    end
end)


2 answers

Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

A number of issues, and they can be easily solved

local canDoDmg = false
local dmg = 25
local db = false

local Plr = game.Players.LocalPlayer
local UserInputService = game:GetService("UserInputService")
local Char = Plr.Character or Plr.CharacterAdded:Wait()
local Humanoid = Char:WaitForChild("Humanoid")
local Anim = Instance.new("Animation")
Anim.AnimationId = "rbxassetid://6203475027"
local track = Humanoid:LoadAnimation(Anim)

UserInputService.InputBegan:Connect(function(InputObject)
    if InputObject.UserInputType == Enum.UserInputType.MouseButton1 and db == false then 
        track:Play()
    canDoDmg = true
    db = true
        script.Parent.RightHand.Touched:connect(function(hit)
            if hit.Parent.Humanoid and canDoDmg == true then
               hit.Parent.Humanoid:TakeDamage(dmg)
            end
        end)
    delay(1, function()
        canDoDmg = false
        db = false
    end)
        wait(10)
    end
end)

Explanation of what I changed: I added a debounce / cooldown to you code to make it so that you cannot spam it (look for the delay thing inside the script) Also made it so that you can only do damage when you punch Also added local in your variables, good coding practice

Let me know if you have any errors

Ad
Log in to vote
0
Answered by
TGazza 1336 Moderation Voter
3 years ago

try this:

canDoDmg = true
dmg = 25

local Plr = game.Players.LocalPlayer
local UserInputService = game:GetService("UserInputService")
local Char = Plr.Character or Plr.CharacterAdded:Wait()
local Humanoid = Char:WaitForChild("Humanoid")
local Anim = Instance.new("Animation")
Anim.AnimationId = "rbxassetid://6203475027"
local track = Humanoid:LoadAnimation(Anim)
local strike = false -- make a variable to stop spamming of our fire button

local TouchedEvent = nil -- make a container for our touched event so we can Disconnect() it later.
UserInputService.InputBegan:Connect(function(InputObject)
    if InputObject.UserInputType == Enum.UserInputType.MouseButton1 then
        if(strike == false) then -- to stop spamming of our fire button
            track:Play()
            strike = true
            if(track.IsPlaying == true) then -- check to see if our punch animation is playing if so hook up our touched event
                TouchedEvent = script.Parent.RightHand.Touched:connect(function(hit)
                    if hit.Parent.Humanoid and canDoDmg == true then
                        hit.Parent.Humanoid:TakeDamage(dmg)
                    end
                end)
            else
                if(TouchedEvent ~= nil) then -- probably not needed but here just encase! :)
                    TouchedEvent:Disconnect() -- Disconnect the touched event when the animation ends 
                end
            end
            wait(10) -- might be a little long before striking again
            strike = false
        end
    end
end)

Hope this helps! :)

0
This one does damage to the player just by walking up to them without playing the animation ItzSulfonic 61 — 3y

Answer this question