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

How can I fix this bug with humanoid.Health?

Asked by 2 years ago

Ok so, I created this script for a sword game. It's basically a rehaul of that one classic Roblox sword. There's currently a bug where you can stack damage. Basically, if you have damaged the humanoid once, then use the sword 5 times, the moment the sword touches the model with the humanoid, it will deal 500 damage. The script is below.

game.ReplicatedStorage.SwordHit.OnServerInvoke = function(player)
    local character = player.Character
    if character then
    local hum = character:FindFirstChild("Humanoid")
    local anim = hum:LoadAnimation(script.Anim) 
    local arm = character:FindFirstChild("Right Arm")
    local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
    local sword = character:FindFirstChild("Sword") -- Sword is part of the StarterCharacter
    local strail = sword:FindFirstChild("SwordTrail")
        local dmg = false
        strail.Enabled = true
        anim:Play()
        sword.SwordSwing:Play()
        wait(0.35)
        strail.Enabled = false
        sword.Touched:Connect(function(hit)
            if hit.Parent:IsA("Model") then
                if dmg == true then return end
                dmg = true
                local hum = hit.Parent:FindFirstChild("Humanoid")
                if (hum ~= nil) then
                hum.Health = hum.Health - 100
                sword.SwordHit:Play()
                end
            end
        end)
    end
end

Any help would be appreciated, Thanks.

1 answer

Log in to vote
0
Answered by
RAFA1608 543 Moderation Voter
2 years ago

The connection sword.Touched:Connect() is getting called multiple times, resulting into multiple connections at once. For example, there are five connections (practically five times more the damage) than there would be if the connection were to be only be connected once.

In general, you shouldn't trust the client, like this script does.

Ad

Answer this question