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)

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

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