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

Can somebody tell me how to make this damage script only activate for 3 seconds when you left click?

I've been trying to figure this out for a while but nothing seems to be working can i have help all i want is so when you left click the damage is active for only 3 seconds, this is just so i don't have to worry about somebody running into the blade and dying without me actually attacking them IMPORTANT: the script is in a part called: blade (no caps) (it's the hitbox of the sword essentially)

local debounce = false 
local HealthLoss = 35 -- Damage
function OnTouched(Part) 
	if Part.Parent ~= nil then 
		if debounce == false and Part.Parent:findFirstChild("Humanoid") ~= nil then 
			debounce = true 
			Part.Parent:findFirstChild("Humanoid"):TakeDamage(HealthLoss) 
			wait(2) 
			debounce = false 
		end 
	end 
end 
script.Parent.Touched:connect(OnTouched)

6 comments

0

a simple debounce can't prevent death from running into a sword

0

cant or can?

0

pretty sure i said can't in my comment

0

k

0

wait wdym debounce i don't think i mentioned it

0

maybe use an Activated event and a GetTouchingParts function or use FindPartOnRay

1 answer

✓ Accepted answer

Use a variable to define if the tool was activated or not. If it wasn't, the "Touched" event would not execute any further.

Final code:

local tool = --your tool
local toolActive = false
local damage = 35
local duration = 3

local function onTouched(hit)
	if not toolActive then do return end end
	local Humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
	if Humanoid then
		Humanoid:TakeDamage(damage)
	end
end

local function onActivated()
	if toolActive then do return end end
	toolActive = true; wait(duration); toolActive = false
end

script.Parent.Touched:Connect(onTouched)
tool.Activated:Connect(onActivated)

4 comments

0

it's not working it says expected identifier, got 'local'

0

What line?

0

line 2 and also all of the 4 things at the top

0

also, do make sure you change line 01 to your where your tool instance is

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