Hello! I’m trying to script damage for a punching tool and i recently thought of a script that would work
local tool = script.parent
local humanoid = tool.parent:FindFirstChild("Humanoid")
tool.touched:connect(function()
if humanoid then
humanoid:takedamage(10)
end
end)
Im currently learning how to code, so i would really appreciate if i receive criticism or tips on what i should do
Firstly, you may want to have an animation play when the player punches and there are a couple of ways to make the script be more efficient. Implementing a debounce between punches, and such. For the damage, it would be ideal if you fire an event instead of handling in that local script.
You may want to look through some tutorials and possibly even some devforum threads, they provide adequate information that help you create systems.
No this will not work. First reason being, .Touched is really unreliable, but this could work, so this is a minor issue. The second reason is not only is the humanoid checking for the players OWN humanoid, but it checks as soon as the player joins, or in other words, while the tool is still in the players backpack. This reason is a minor issue that will not cause any difference, but you aren’t using Tool.Activated, meaning that the player may hit someone (or themselves) even if they touched them without using the tool. The fix to this is to either raycast to the nearest player, which may be too advanced for you, or to use .Touched in a different way.
local tool = script.Parent --The tool
local damage = 10 --Damage punching does
local player = game.Players.LocalPlayer -- The player (assuming you're using a local script)
local character = player.Character or player.CharacterAdded:Wait() --Getting the player's character
tool.Activated:Connect(function() --Checking when the tool is activated
local touchingParts = character["Left Arm"]:GetTouchingParts() --Assuming you're using R6 (because R15 bad), and you are punching with your left arm
local ownHumanoid = character.Humanoid --Your Humanoid
for _, part in ipairs(touchingParts) do --Looping through all touching parts
if part.Parent:FindFirstChild("Humanoid") and part ~= ownHumnaoid then --Checking if the player hit a model with a humanoid and if that model isnt the player's own
local otherHumanoid = part.Parent.Humanoid --The humanoid to damage
otherHumanoid:TakeDamage(damage) --Damaging the humanoid
end
end
end)
Here is a script I made to show you what a good way to do this would be. You would also want to use animations and change the script up a bit, but this was just to give you an idea of what you would want to do!
Debouncing is like a delay, for example, lets say we have a bool called CanAttack. When the player uses a tool, the script will check if CanAttack is true. If it is, set it to false, else return the script. Once the script is done, set that value back to false.
local CanAttack = true -- The bool
tool.Activated:Connect(function() --Checking when the tool is activated
if CanAttack then --Checking if CanAttack is true
CanAttack = false --Setting to false
print("I Have attacked") -- "Attacking"
wait(2) --Waiting
CanAttack = true --Can attack now
else
print("I can not attack yet :(") --nice
end
end)
Thank you so much! I really needed this, is it okay if i use this script (i’ll try to change it a bit and i’ll use this script for educational purposes and try to understand the parts i dont know)
From me just looking at it for like a second, nothing seems wrong. Although, you would want to get the humanoid how I did in the script I sent you as it is much more reliable, to me at least. I’ve never tried getting the humanoid that way. Anyways you would also just want to do playanim:Play(), then one line after playanim.Stopped:Wait(), unless it is stopped by another script, this will yield (or pause) the script until the animation is stopped, which can be used as a check for when the animation is completed. You can get rid of the playanim:Stop() part as the animation automatically stops if it has been completed, unless the animation is looped. Other than that this script looks fine to me, if it does error, add print() statements to define where the script breaks, and send me the output and the line it breaks on.
There is a number of ways. game.Players:GetPlayerFromCharacter() returns the player from their character, game.Players.PlayerAdded returns the player when they join, RemoteEvents, RemoteFunctions. Etc. On the server you just need to get creative to find the player, as the Server doesn’t have immediate access to the player, because it isn’t the player, where as local scripts, or clients, ARE the player (kind of).
local cooldown = false --The cooldown
if not cooldown then
cooldown = true
--Do stuff if they can
wait(2)
cooldown = false
else
--(Optional) do stuff if they cant
end