Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Damage Can Be Spammed, and I Can't Figure Out Why?

So basically whenever you hit the player you can keep on clicking the weapon which deals damage, I want it so you can only deal the damage 1 time if the weapon is activated?

Any Ideas Why My Script Is Doing This?

server script in Tool

Av = script.Parent.DamageValue.Value --Attack Value 
local CanDamage = script.Parent.CanDamage
tool = script.Parent

function tagHumanoid(humanoid,killer)

    if humanoid and killer then

        local tag = Instance.new("ObjectValue")
        tag.Name = "Killer"
        tag.Value = killer
        tag.Parent = humanoid
    end

end

script.Parent.Activated:Connect(function()
	
	CanDamage.Value = true
	
	if script.Parent.Handle.Touched then
		
		script.Parent.Handle.Touched:Connect(function(p)

	local plr = game.Players:GetPlayerFromCharacter(tool.Parent)

	if script.Parent.CanDamage.Value == true and p.Parent:FindFirstChild("Humanoid") then

    script.Parent.CanDamage.Value = false

    local humanoid = p.Parent:FindFirstChild("Humanoid")

    humanoid:TakeDamage(Av)

    tagHumanoid(humanoid, plr)

    wait(.2)

    script.Parent.CanDamage.Value = true

    if p.Parent.Humanoid.Health < 1 then
        print("Player Has Died")
    end

    end
		end)
		end
end)

1 comment

0

There is only 1/5th of a second between each time being able to fire

1 answer

What MrLonely1221 said is right. You state


wait(.2)
 	 
script.Parent.CanDamage.Value = true

after setting CanDamage.Value to false, thus you can deal damage every 0.2 seconds. To fix this, just never set CanDamage.Value to true. Only do it in the beginning of your function, like you have.

Further more your indenting scares me. http://blackmiaool.com/lua-beautify/ can help you learn how to indent and make your code 10x prettier.

5 comments

0

Even though I removed the part where the Value turns true, I'm still able to spam the weapon and deal the damage?

0

Do you want the weapon to be able to deal damage every time you equip it or just once overall?

0

Another thing you can do is to increase the wait time.

0

I want it to deal damage every time you equip it, but only once every click

0

I've tried changing wait time? Still allows you to do a lot of damage by spamming

This discussion is preserved as it was. New answers, comments, and votes are closed.