Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How can I add a damage script to this punch script?

So I recently decided to make a script that lets a player punch with animation and deal dmg

I was able to make the punch animation work with the Q key (even did the debounce with the help of a friend) but I couldnt do the damage part anyone mind helping me with that

This is my script

local userInputService = game:GetService("UserInputService")
local replicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
local Player = game.Players.LocalPlayer
local Character = Player.Character

 local Anim = Instance.new('Animation')
  Anim.AnimationId = 'rbxassetid://4588745607'
  PlayAnim = Character.Humanoid:LoadAnimation(Anim)
PlayAnim.Priority = Enum.AnimationPriority.Action




function debounce(func)
  local isRunning = false
  return function(...)
    if not isRunning then
      isRunning = true
      func(...)
      isRunning = false
    end
   end
end

userInputService.InputBegan:Connect(debounce(function(input)
  if input.KeyCode == Enum.KeyCode.Q then
    PlayAnim:Play()
    wait(0.75)
    PlayAnim:Stop()
wait(0.25)
  end
end))

How can I implement a damage script to this?

1 comment

0

So you just need damage?

1 answer

You could insert a bool to check if the player is swinging as well as one to limit the damage per second. After that you can insert a .Touched to it. Here's a code that should work for you.

local userInputService = game:GetService("UserInputService")
local replicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Swinging = false
local DamageCD = false

 local Anim = Instance.new('Animation')
  Anim.AnimationId = 'rbxassetid://4588745607'
  PlayAnim = Character.Humanoid:LoadAnimation(Anim)
PlayAnim.Priority = Enum.AnimationPriority.Action




function debounce(func)
  local isRunning = false
  return function(...)
    if not isRunning then
      isRunning = true
      func(...)
      isRunning = false
    end
   end
end

userInputService.InputBegan:Connect(debounce(function(input)
  if input.KeyCode == Enum.KeyCode.Q then
    PlayAnim:Play()
    Swinging = true
    PlayAnim.Stopped:Wait()
    Swinging = false
  end
end))

Character.RightHand.Touched:Connect(function(hit)

if Swinging == true and DamageCD == false and hit ~= nil and hit.Parent ~= nil and hit.Parent:FindFirstChild("Humanoid") then

DamageCD = true
hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 10

end)

This discussion is preserved as it was. New answers, comments, and votes are closed.