Why won't this damage script work?

So I’ve got this gun bash script that when I press V, it will play a bash animation, and enables the disabled damage script that’s inside the gun. The thing is that the script doesn’t work. I’ve tried turning on CanTouch and switching scripts but it doesn’t work.

Code:

function onTouched(hit)
        local human = hit.Parent:findFirstChild("Humanoid")
        if (human ~= nil) then
                human.Health = human.Health -100 -- Change the amount to change the damage.
        end
end
script.Parent.Touched:connect(onTouched)
1 Like
local debounce = false
function onTouched(hit)
        local human = hit.Parent:findFirstChild("Humanoid")
        if human then
              if debounce == false then
                      human.Health -= 100 -- Change the amount to change the damage.
                     wait("amount of time in seconds")
                     debounce = true
              end
        end
end
script.Parent.Touched:connect(onTouched)

it doesn’t seem to work? Maybe it’s the code that does plays the animation.

local Player = game.Players.LocalPlayer


Character = Player.Character or Player.CharacterAdded:Wait()

local Humanoid = Character:WaitForChild("Humanoid")

local bash = false

bashAnimation = Humanoid:LoadAnimation(script:WaitForChild("BashHit"))

script.Parent.Equipped:connect(function(mouse)

	mouse.KeyDown:connect(function(key)
		key = key:lower()
		if key == "v" then
			
			script.Parent.WoodParts.HitScript.Disabled = false
			bashAnimation:Play()
			
			
			
		end
	end)
end)

May be try to use UserInputService, or, maybe try to type V instead of v?
Btw if the script was a server script, make it into a local script inside starterplayer. cuz you can’t get a local player from a server script

its a local script inside the gun itself.

is the gun parented to the workspace or serverstorage? the local script has to be a descendant from a player’s backpack, character, playergui, or the player’s playerscripts in order to work.

it’s parented to the gun itself

function onTouched(hit)
    local Humanoid = hit.Parent:FindFirstChild("Humanoid");
    if Humanoid then
        Humanoid:TakeDamage(100);
    end
end

script.Parent.Touched:Connect(onTouched)

Can you add a print statement to check whether the damage script gets enabled or not?

print("Damage script is enabled")
function onTouched(hit)
        local human = hit.Parent:FindFirstChild("Humanoid")
        if (human ~= nil) then
                human.Health = human.Health -100 -- Change the amount to change the damage.
        end
end
script.Parent.Touched:Connect(onTouched)

If you’re going to make a gun to take damage of other players, I would suggest using Remote Events.

It’s the same thing of doing :Connect(onTouched).

nothing in the output, but the disabled value is off, so yes?

You’re right nevermind.

-ignore this-

Didn’t you say the parent of the script is the gun itself? Then it doesn’t make sense that the Touched event is connected to the gun, because you’re throwing a bullet at the player, not a gun.

im not making the gun fire? it’s a gun bash, not a gun shoot

Sorry, but what does that mean exactly?

the damage script is inside a part of the gun, the bash script plays an animation that moves that part of the gun with the damage part in it into an enemy player which damages them

Oh, so you mean DASH not bash, right?

its pistol whip, but i think gun bash works better, either way, im trying to make the gun as a blunt weapon if an enemy gets too close instead of just shooting

I see what you want do. Anyway, damaging players should always be server sided, not in a local script.
You might wonna use a Remote Event for this, you can look up a tutorial on youtube.

If you damage a player in a local script, only you will see the player getting damaged or die. For other players, the player you damaged/killed was never damaged.