Why wont my punch damage script work?

1. What do you want to achieve? Keep it simple and clear!
I am trying to make a punch script.

2. What is the issue? Include screenshots/videos if possible!
The damage script is completely not working

3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I tried using magnitude and touched events.

edit:
I’ve been trying to make a damage script for my punch script through a remote event and its not working. I’ve tried Magnitude and touched events for my character’s hands, feet, arms, and legs, but none seem to work. It is an animation based script. (The reason why I am using pcalls is because I keep getting these annoying errors saying stuff about the animation and how it is my friends animation.)

Animation "https://assetdelivery.roblox.com/v1/asset?id=4149415551&serverplaceid=0" failed to load in "Animation.AnimationId": Animation failed to load

local HitSound = {'1693499499'}
local MissSound = {'1489705211'}

local start = 'http://roblox.com/asset/?id='

local start2 = 'rbxassetid://'

game.ReplicatedStorage.Events.AttackPunch.OnServerEvent:Connect(function(Player)

local p = pcall(function()
local UsedPunchId = PunchAnimationIDS[math.random(1, #PunchAnimationIDS)]
local UsedHitId = HitSound[math.random(1, #HitSound)]

local Animation = Instance.new("Animation")
Animation.AnimationId = start2..UsedPunchId


local ANIM = Player.Character.Humanoid:LoadAnimation(Animation)

ANIM:Play()
game.Debris:AddItem(Animation,3)

local MissHit = Instance.new("Sound",Player.Character.HumanoidRootPart)
MissHit.SoundId = start2..MissSound[math.random(1, #MissSound)]
MissHit:Play()
game.Debris:AddItem(MissHit, 10)

for _, v in next(Player.Character:GetChildren()) do
	if v.Name:match('Arm') or v.Name:match('Hand') or v.Name:match('Foot') or v.Name:match('Leg') then
		v.Touched:Connect(function(hit)
			if hit and hit.Parent and hit.Parent:FindFirstChild('Humanoid') then
				local HitSound = Instance.new("Sound",Player.Character.HumanoidRootPart)
				HitSound.SoundId = start2..HitSound[math.random(1, #HitSound)]
				HitSound:Play()
				MissHit:Pause()
				game.Debris:AddItem(MissHit, 0.0001)
				game.Debris:AddItem(HitSound, 10)
				hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 8
				print('hit boi finally') 
			end
		end)
	end
end


end)
end)```

It is fired from the local player.

Don’t know if any of this is explicitly your problem since nothing’s been explained well (e.g. no mentions of the console) and there aren’t any notions of prior attempts by you to debug using any of the available debugging tools (e.g. prints, breakpoints).

There are a few practice related issues in your code to go over, besides potential real issues. There are also suggestions to be made. I will break it down for you.


I. Redundancy from get-go.

Start and start2 are exactly the same. rbxassetid is a shorthand for the asset domain. Keep one and remove the other. I suggest using rbxassetid as that is more common and easier to work with. I wouldn’t exactly say it’s canonical but it is highly preferred. Studio converts assets to this link when inputted as well.


II. Improper use of pcalls.

Yes, pcall is meant to run something in protected mode in order to catch errors and allow you to do custom error handling. You should not, however, be wrapping your entire code in pcall. You shouldn’t be using it all in your scenario. I’d remove it.


III. Intervals for math.random can be shortened.

Instead of writing out math.random(1, #table), you can simply write math.random(#table). This will automatically set the interval maximum as the sole value you passed and the minimum will be 1.


IV. Using the parent argument of Instance.new or setting parent before other properties.

Don’t.


V. Method of iteration.

I’m not too sure where developers created the habit of skipping using a generator like pairs or ipairs and went straight for next, but I’m not sure how I feel about that. You should try and use ipairs as it is designed for array iteration.

Next and pairs are more for dictionaries where keys are arbitrary and unknown from the start. With ipairs, you get to work with an [i+1] basis with guaranteed order during iteration.


That’s all I can offer you. You’ll need to provide more information if you require further help on this issue. You can also try just debugging this yourself.

2 Likes

I’ve been trying to make a damage script for my punch script through a remote event and its not working. I’ve tried Magnitude and touched events for my character’s hands, feet, arms, and legs, but none seem to work. It is an animation based script. (The reason why I am using pcalls is because I keep getting these annoying errors saying stuff about the animation and how it is my friends animation.)
Animation "https://assetdelivery.roblox.com/v1/asset?id=4149415551&serverplaceid=0" failed to load in "Animation.AnimationId": Animation failed to load

Your problem is that the animation is not yours. You can only use animations if it’s owned by you or you’re in a group game using the group’s animations.

I know it’s not mine The main focus is the damage script…

The script won’t continue because you don’t own the animation, you will need to remove that line or edit the ID to your animation.

The script will continue, the animation just won’t play.

2 Likes

This is small, but you should try using Humanoid:TakeDamage() rather than setting the humanoid’s health manually.

I think it’s worth reinforcing my point here because this isn’t the kind of elaboration or point I was hoping you’d get out of my response.

Please attempt to debug this code yourself without the pcall which is muting your stack trace. That or add a second variable beside p and print both returns of the pcall after it runs.

It is imperative that you do your own debugging before writing a thread, as a lot of issues you stumble across may be easily solvable given time spent away at it.

1 Like

That’s not the point though. An animation that fails to load will throw an error but it doesn’t terminate the thread. As OP is not relying on any of the AnimationTrack APIs, the only thing that won’t happen is that the arms won’t animate. This is therefore unrelated and off-topic.

The main focus is the damage script. Animation ownership is far from the problem and can be rectified very quickly by switching out for a placeholder animation by Roblox or OP.

Never mind I’ve figured it out! Thank you for the support on this and I’ll be sure to bookmark your topics.

Please share the steps you took to figure out the problem so others may refer to it should they encounter a similar issue as yours.

2 Likes