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

Bleed chance on weapons?

Asked by 5 years ago

I am making some weapons, with chances of having bleeding in them. For this example i use the Basic Knife, which has the chance of 20% to bleed for 20 seconds, dealing 1 damage every second, making that a total 20 damage.

I was trying to make this, there are no errors in the script, but for some reason the script makes a 100% bleedchance, and the bleeding only deals a total of 1 damage.

This is my script:

-- Main variables

local hitbox = script.Parent
canDamage = true
canEffect = true

-- Configurations

damage = 35
reload = 0.75
effectChance = 2 --/10

-- Attack

hitbox.Touched:connect(function(hit)
    if (hit.Parent.ClassName == "Model") then
        if (hit.Parent:FindFirstChild("HumanoidEnemy")) then
            if canDamage == true then
                hit.Parent.HumanoidEnemy:TakeDamage(damage)
                print(hit.Parent.Name .. " took " .. damage .. " damage")

                local randomEffectChance = math.random(1, 10)
                print("The number of randomEffectChance = " .. randomEffectChance)
                if randomEffectChance == 1 or 2 then
                    if canEffect == true and canDamage == true then
                        canEffect = false
                        print(hit.Parent.Name .. " got an effect")
                        for i = 0, 20, 1 do
                            hit.Parent.HumanoidEnemy:TakeDamage(1)
                            print(hit.Parent.Name .. " is taking 1 damage")
                            wait(1)
                        end
                    end
                end

                canDamage = false
                wait(reload)
                canDamage = true
            end
        end
    end
end)

BTW, i made the enemy humanoid another name so you cant damage teammates. I called them "HumanoidEnemy", and there is another working script that makes the animations.

Thanks in advance

0
Shouldn't `if randomEffectChance == 1 or 2 then` be `if randomEffectChance == 1 or randomEffectChance == 2 then`? fredfishy 833 — 5y

1 answer

Log in to vote
1
Answered by
fredfishy 833 Moderation Voter
5 years ago
Edited 5 years ago

So the line if randomEffectChance == 1 or 2 then should be if randomEffectChance == 1 or randomEffectChance == 2 then

They way you've got it at the moment is basically if ((randomEffectChance == 1) or (2)) then

Since 2 is a "truthy" value, it basically means that if 2 then always fires, in the same way that if 1 then always fires.

So what you have here is

((randomEffectChance == 1) or (2))

which evaluates to

((randomEffectChance == 1) or (true))

and since the right hand side of the or is always true, the whole thing is always true, giving

if true then

explaining why it always fires.

1
omg you are amazing, thanks so much for helping :D Bram1507 8 — 5y
0
Sir! It worked! But i have one other question... Why does my for loop only executes once and does not loop 20 times? Because the zombie im testing it on is only getting 1 damage from the effect in total? Bram1507 8 — 5y
0
Of that I'm not so sure - what does the output say? Does it say "taking damage" 20 times or just once? fredfishy 833 — 5y
0
oh hi, sorry for posting lately, i didn't got noticed. uhh it only says that once Bram1507 8 — 5y
View all comments (2 more)
0
The following seems to work? https://pastebin.com/ZCs6CBQf Not really sure what the problem is because I didn't change anything but your code works on my end. fredfishy 833 — 5y
0
oh hi again, i was away im sorry for answering so late again XD, if it works on your roblox studio, it should do it one mine, i think i do something wrong. At least thanks for helping and again sorry for answering so lately. Bram1507 8 — 5y
Ad

Answer this question