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

Punch tool does damage when not clicking?

Asked by 4 years ago

I'm trying to make a good punch tool but even though it is unequipped and I am not clicking it still does damage to the enemy NPCs. How can I fix this? (script) I've already tried things like: -tool.Activated -debounce

Here's the script:

local tool = script.Parent
local hitsound = tool:WaitForChild("Hit")
local canattack = tool:WaitForChild("CanAttack")
local plr = tool.Parent.Parent
local char = plr.Character or plr.CharacterAdded:wait()
local larm = char:WaitForChild("LeftHand")
local rarm = char:WaitForChild("RightHand")

larm.Touched:Connect(function(hit)
    local hum = hit.Parent:FindFirstChild("Humanoid")
    if canattack.Value == false then
        if hum then
            canattack.Value = true
            hitsound:Play()
            hum:TakeDamage(10)
            wait(.5)
            canattack.Value = false
        end
    end
end)
rarm.Touched:Connect(function(hit)
    local hum = hit.Parent:FindFirstChild("Humanoid")
    if canattack.Value == false then
        if hum then
            canattack.Value = true
            hitsound:Play()
            hum:TakeDamage(10)
            wait(.5)
            canattack.Value = false
        end
    end
end)

1 answer

Log in to vote
1
Answered by
iuclds 720 Moderation Voter
4 years ago

You should try raycasting renderstep on both arms, and if it finds a humanoid, the humanoid takes 10 damage. https://developer.roblox.com/en-us/articles/Making-a-ray-casting-laser-gun-in-Roblox https://devforum.roblox.com/t/raycasting-how-do-i-do-it/186446

1
This would be your best bet. And for future reference, the "touched" event runs every time you touch something so you would need to somehow verify it is in equipped before taking damage. greenhamster1 180 — 4y
0
Thanks! It works SamuelaNutella 14 — 4y
Ad

Answer this question