Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 6 years ago

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)

01local debounce = false
02local HealthLoss = 35 -- Damage
03function OnTouched(Part)
04    if Part.Parent ~= nil then
05        if debounce == false and Part.Parent:findFirstChild("Humanoid") ~= nil then
06            debounce = true
07            Part.Parent:findFirstChild("Humanoid"):TakeDamage(HealthLoss)
08            wait(2)
09            debounce = false
10        end
11    end
12end
13script.Parent.Touched:connect(OnTouched)
0
a simple debounce can't prevent death from running into a sword theking48989987 2147 — 6y
0
cant or can? Averted_Vision 177 — 6y
0
pretty sure i said can't in my comment theking48989987 2147 — 6y
0
k Averted_Vision 177 — 6y
View all comments (2 more)
0
wait wdym debounce i don't think i mentioned it Averted_Vision 177 — 6y
0
maybe use an Activated event and a GetTouchingParts function or use FindPartOnRay theking48989987 2147 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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:

01local tool = --your tool
02local toolActive = false
03local damage = 35
04local duration = 3
05 
06local function onTouched(hit)
07    if not toolActive then do return end end
08    local Humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
09    if Humanoid then
10        Humanoid:TakeDamage(damage)
11    end
12end
13 
14local function onActivated()
15    if toolActive then do return end end
16    toolActive = true; wait(duration); toolActive = false
17end
18 
19script.Parent.Touched:Connect(onTouched)
20tool.Activated:Connect(onActivated)
0
it's not working it says expected identifier, got 'local' Averted_Vision 177 — 6y
0
What line? Wicked_Wlzard 110 — 6y
0
line 2 and also all of the 4 things at the top Averted_Vision 177 — 6y
0
also, do make sure you change line 01 to your where your tool instance is Wicked_Wlzard 110 — 6y
Ad

Answer this question