I have tried to learn how to use f to punch, when I finished it works but it activates 24 times on impact (found out using a print statement), how would I properly use debounce to set this up, because I looked google and looked on devforums and understood it but it never worked for me.
here is my code
Add a and db == false
in the if statement for your .Touched function.
It might be better to rename it to “attacking” instead of db though.
Where was the print statement? I don’t see a debounce in the RightHand Touched function, so that one could cause damage multiple times. Have you tried debouncing there too?
I removed it, just so I could check how much for this devforum post, sorry for not saying that in the thread. can you show me a example of how to put debounce
would you mind giving me a example because i’m not the best with debounce
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local rootPart = humanoid.RootPart
local rightHand = character:WaitForChild("RightHand")
local anim = Instance.new("Animation")
anim.Animationid = "rbxassetid://7285345698"
local animation = animator:LoadAnimation(animation)
local attacking = false
UserInputService.InputBegan:Connect(function(inputObject)
if inputObject.KeyCode == Enum.KeyCode.F and not attacking then
attacking = true
animation:Play()
animation.Stopped:Wait()
attacking = false
end
end)
rightHand.Touched:Connect(function(hit)
local enemy = hit.Parent
local enemyHumanoid = enemy and enemy:FindFirstChild("Humanoid")
if enemyHumanoid and attacking then
enemyHumanoid:TakeDamage(15)
end
end)
Even better:
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local rootPart = humanoid.RootPart
local rightHand = character:WaitForChild("RightHand")
local anim = Instance.new("Animation")
anim.Animationid = "rbxassetid://7285345698"
local animation = animator:LoadAnimation(animation)
local attacking = false
UserInputService.InputBegan:Connect(function(inputObject)
if attacking == true then
return
end
attacking = true
if inputObject.KeyCode == Enum.KeyCode.F then
animation:Play()
animation.Stopped:Wait()
attacking = false
end
end)
rightHand.Touched:Connect(function(hit)
if attacking == true then
return
end
attacking = true
local enemy = hit.Parent
local enemyHumanoid = enemy and enemy:FindFirstChild("Humanoid")
if enemyHumanoid then
enemyHumanoid:TakeDamage(15)
attacking = false
end
end)
I prefer compact but sure. You can even simplify it to this. Also, I didn’t choose this way because I didn’t want to treat attacking as a debounce, since it’s used as a variable instead to activate the damage portion. This makes it a little misleading.
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local rootPart = humanoid.RootPart
local rightHand = character:WaitForChild("RightHand")
local anim = Instance.new("Animation")
anim.Animationid = "rbxassetid://7285345698"
local animation = humanoid:LoadAnimation(animation)
local attacking = false
UserInputService.InputBegan:Connect(function(inputObject)
if attacking then return end
attacking = true
if inputObject.KeyCode == Enum.KeyCode.F then
animation:Play()
animation.Stopped:Wait()
end
attacking = false
end)
rightHand.Touched:Connect(function(hit)
if attacking then return end
local enemy = hit.Parent
local enemyHumanoid = enemy and enemy:FindFirstChild("Humanoid")
if enemyHumanoid then
enemyHumanoid:TakeDamage(15)
attacking = false
end
end)
This way works too. But I disagree. I think attacking should be like a debounce, but thats fine. We all have different ways we do stuff.
I was about to fix the code to make it compacted. lol.
this didn’t work for me. when I clicked f nothing happened. i have the script in startercharacterscripts, so is that a issue? or is that fine
It was meant for StarterCharacterScripts so that works. Did you get my latest version? Try copying and pasting it again. You can also put print("Hello")
in places to see if those parts of the code get reached.
It might be because there is no Animator instance. I’ll update the code.
Instead of creating the animation from the client (localscript), make the animation and put it in ReplicatedStorage or something. Then, load that animation.
I found out that the animator is needlessly complicated despite it being the proper way to animate, so I’m removing it from the script and using the deprecated Humanoid:LoadAnimation().
Yes. Change the old anim to local anim = game:GetService("ReplicatedStorage").Animation
Both of yours didn’t do anything for some reason
Still didn’t work, i was having issue with multi hits and now it just doesn’t work in general
the reason it activates 24 times is because when the hand is touched it will continue to detect whenever another part touches it because you don’t tell it to stop detecting touches so if you make another debounce for when the hand is touched it should work.