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

Why is this script not working as it is supposed to be doing? (It is a sword/baton tool)

Asked by 5 years ago
local AbleToDamage = true

local Damage = 30



script.Parent.Handle.Touched:Connect(function(Hit)

script.Parent.Activated:Connect(function()

if AbleToDamage == false then return end

AbleToDamage = false

if Hit.Parent:FindFirstChild("Humanoid") then

Hit.Parent.Humanoid:TakeDamage(Damage)

end

wait(0.5)

AbleToDamage = true

end)

end)

I know that the problem is that the hit value saves. How do I fix it so it looks for a new value each time I activate the tool?

0
Please provide a bit more background detail. UnitedGateCompany 18 — 5y

1 answer

Log in to vote
0
Answered by
CjayPlyz 643 Moderation Voter
5 years ago
Edited 5 years ago
local AbleToDamage = true
local Damage = 30

local function Activated ()
    script.Parent.Handle.Touched:Connect(function(Hit)
        if AbleToDamage == true then
            AbleToDamage = false
            local Humanoid = Hit.Parent:FindFirstChildWhichIsA("Humanoid")
            if Humanoid then
                Humanoid:TakeDamage(Damage)
            end
            wait(0.5)
            AbleToDamage = true
        end
    end
end

script.Parent.Activated:Connect(Activated)

I noticed a problem, if you activate the tool it would listen for the touched event but after that it would still continue to listen for the touched event, so if your still holding the tool if someone touches it, it would damage them. I didn't really know what was wrong with it, so i tried to re-create it and while doing so i also tried to "improve" it.

You can try this, i added something so that it doesn't still listen for the touched event after its activated.

local AbleToDamage = true
local Damage = 30
local Connection

local function Activated ()
    Connection = script.Parent.Handle.Touched:Connect(function(Hit)
        if AbleToDamage == true then
            AbleToDamage = false
            local Humanoid = Hit.Parent:FindFirstChildWhichIsA("Humanoid")
            if Humanoid then
                Humanoid:TakeDamage(Damage)
            end
            Connection:Disconnect()
            wait(0.5)
            AbleToDamage = true
        else
            Connection:Disconnect()
        end
    end)
end

script.Parent.Activated:Connect(Activated)
0
I don't think the hit value is the problem.. well anyway i think this is right.. i think, oh and you should use animations. CjayPlyz 643 — 5y
0
if your gonna use them, then put the code to play it right after "script.Parent.Handle.Touched:Connect(function(Hit)" CjayPlyz 643 — 5y
Ad

Answer this question