Would this script work?

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 :sweat_smile:

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.

How to make this punching script deal damage - Scripting Support - DevForum | Roblox

Fyi; that may need configuring to make it work with a tool.

1 Like

yeah, my friend and i are collaborating so he’s also scripting, and he implemented script for the animations

(heres the script incase you may need it)

But i have no idea what debouncing means :sweat_smile: i barely just started coding haha

(i didnt write this code, my friend Shonny_Yeet did)

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

3 Likes

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)
2 Likes

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)

1 Like

Yeah I don’t mind, though it may not work as I just typed it here lol. Good luck with this!

Edit: Make sure this is a LocalScript or else game.Players.LocalPlayer won’t work.

1 Like

Also, i know its off topic but the script my friend Shonny_Yeet wrote

(this one)

use to work when we playtested the game but now it doesnt, is there anything wrong with it so i could fix it?

How would i use this if i wasnt using a local script?

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).

1 Like

@astrozzyz

Hey, thanks for your help by the way, but when i took your advice the script still didnt work :pensive:

here’s the script and the output

Umm. Can you show the explorer tab of the system stuff, like what it needs to function (picture), and the full code?

1 Like

Sure! Hold on

Sorry didn’t get notified, the error states that CoolDown doesn’t exist. So either you can just do local CoolDown = false or use :WaitForChild()

@astrozzyz

Can you please show me an example, i dont really understand haha :sweat_smile:

1 Like
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
1 Like