But me and my friend Shonny_Yeet created a script for a right punch, the animation plays but the damage script doesnt seem to be running even though there are no errors in the scripts output
heres the script
--Local stuff
local tool = script.Parent
local CoolDown = script.Cooldown.Value
local animation = script.Animation --Put the name of the animation here!
--Config
--You can change Cooldown.
tool.Activated:Connect(function()
local humanoid = tool.Parent:FindFirstChildWhichIsA('Humanoid')
if humanoid and CoolDown == false then
CoolDown = true
local playanim = humanoid:LoadAnimation(animation)
print("Working 2!")
playanim:Play()
print("Working 3!")
wait(0.5)
playanim:Stop()
print("Working 4!")
wait(0.4)
print("Working 5!")
CoolDown = false
print("Working 6!")
--Damage Script Below
local damage = (10) --damage amount
local player = game.Players.PlayerAdded --the player
local character = player.Character or player.CharacterAdded() --the character
tool.Activated:Connect(function() --Connecting the damaging script
local touchingParts = character["Right Arm"]:GetTouchingParts()
for i,v in ipairs(touchingParts) do --looping through the table
print(i..v)
local OtherHumanoid = tool.Parent.Humanoid
OtherHumanoid:TakeDamage(damage)
end
end)
end
end)
The damaging script was created by @astrozzyz , they allowed me to use it and i made some slight changes
When i play the game, this is what the output looks like
and here’s the actual game
The dummy im hitting wont take damage and im not so sure why, ive barely started coding and im confused on how i can fix this problem
--Local stuff
local tool = script.Parent
local CoolDown = script.Cooldown.Value
local animation = script.Animation --Put the name of the animation here!
local damage = 10
local player = game.Players.LocalPlayer
local character = player.Character
local connection = nil
--Config
--You can change Cooldown.
tool.Activated:Connect(function()
local humanoid = tool.Parent:FindFirstChildWhichIsA('Humanoid')
if humanoid and CoolDown == false then
CoolDown = true
local playanim = humanoid:LoadAnimation(animation)
print("Working 2!")
playanim:Play()
connection = character["Right Arm"].Touched:Connect(function(h)
if h.Parent:FindFirstChildOfClass("Humanoid") then
local OtherHumanoid = h.Parent.Humanoid
OtherHumanoid:TakeDamage(damage)
end
end)
print("Working 3!")
wait(0.5)
playanim:Stop()
print("Working 4!")
connection:Disconnect()
wait(0.4)
print("Working 5!")
CoolDown = false
print("Working 6!")
end
end)
A Class is what kind of thing the object is. For example, a Union’s class is “UnionOperation”. A part’s class is “Part”, ClassNames are unique, and can’t be changed, unlike normal Names.
FindFirstChildOfClass looks for the ClassName unlike FindFirstChild which finds the Name.
The player variable is gotten when it is in the backpack, not in the character, since it’s not in the Activated function. Move it into your Activated function.
It looks like you have an actual connection to the .Touched event (connection = character["Right Arm"].Touched:Connect(function(h)). Maybe make it so, if you want to hit one person only instead of multiple, when you hit someone disconnect the function like this connection:Disconnect(). If you want to keep multiple hits then disconnect it at the end of the script (when you set the CoolDown to false basically). If you don’t disconnect it, it will keep damaging other players even when the animation stopped and the CoolDown is set to false.
Sure!
If you didn’t understand the connection thing, if you look at line 21 there’s a connection = character… variable, which defines the .Touched function. You can use that to disconnect the Touched function and actually stop detecting hits. To do so, you can use connection:Disconnect().
Let’s completely remove the damage when the animation stops. At line 30, you can put the same thing as before, connection:Disconnect(). This will allow you to stop detecting hits after the animation has stopped.
But right now, since we’re disconnecting it only after the animation has stopped, it will keep damaging humanoids, even the ones it has already hit. To fix this, we have 2 choices:
Damage multiple times, but not the already hit players. To do so, we can use tables. local alreadyHit = {}
When the Humanoid of a player has been damaged, you can use table.insert(alreadyHit,OtherHumanoid)
And to actually prevent the damage to the already hit humanoids, you’ll have to put an if statement to check it before the OtherHumanoid is damaged.
local OtherHumanoid = h.Parent.Humanoid
if table.find(alreadyHit, OtherHumanoid) then
return
end
OtherHumanoid:TakeDamage(10)
Completely stop damaging other humanoids when one is hit. We can use only a variable, or a debounce, for this. local hasHit = false
Then check if there already was a hit (in the Touched function) with
if hasHit then
return
end
local OtherHumanoid = h.Parent.Humanoid
OtherHumanoid:TakeDamage(10)
I’d show you better if I had your script written instead of an image, but I hope I gave the idea on how to do it.