How do you make a script where a sword can do critical hits from time to time

How would you make a script where using a sword, there’s a chance for a critical hit, like 2x damage or something.

Note: I’m thinking of using a value, and percentage of chance of doing the crit. When it happens, it changes the value which is the damage.

1 Like

A way you could do it would be a math.random before the damage part. Say you want it to do a crit 20% of the time, you’d do either this

local chance = math.round(100/20) -- Where 20 is the chance of a crit

if math.random(chance) == 1 then
    Humanoid:TakeDamage(damage*2)
else
    Humanoid:TakeDamage(damage)
end

or this if you want to shorten it a bit

local chance = math.round(100/20) -- Where 20 is the chance of a crit

local newDamage = math.random(chance) == 1 and damage * 2 or damage

Humanoid:TakeDamage(newDamage)

Where Humanoid is the variable containing the Humanoid to damage, and damage being the damage to deal

They both do the same thing, you can use the first one if it’s simpler for you to understand if needed. This is just a basis of h ow you can implement critical hits

2 Likes

Was making a sword fighting game for my friend. Thanks!

welcom

1 Like

Anytime! If you have anymore issues don’t be afraid to make another post!

1 Like