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

How do i fix my punch script?

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

✓ Accepted answer

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

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! :)

1 comment

0

This one does damage to the player just by walking up to them without playing the animation

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