Help with roadblock?

Hey there! So, i found another roadblock myself, an attempt to fix this roadblock was made but didn’t work.

Lets see so, i have a script where you click on a part and every one second it takes damage and gives you currency. But theres a problem, im using:

while v1 == false do
task.wait(1)
end

so theres a 1 second delay when i first click it and i don’t want that. If i do:

task.wait()

instead then it would just instantly damage the part all the way and give the player a million currency.

I tried thinking a ton but i can’t just find a solution myself with my current skill level on scripting.

Also, another thing is that i want the player to be given currency every 25% damaged, i tried:

local Percent = (tonumber(dc.Value) / tonumber(ndc)) * 100
										
if Percent >= module.Percent then
	Percent = 0
	module:GiveCurrency(plr,coin,rt,"Coin") --plr,coin model,amount,currency
end

but ofc it didn’t work since it just damaged so much that it broke the part instantly and gave the player a bunch of coins since the wait is like nothing.

Thanks!!

Is the goal to make it so the player can only damage the part once per second, or is instead meant to be if the player clicks it once, it just keeps taking damage every second?

1 Like

Ah, right, so the goal is to make the part take damage i’d say about every second and every time the coin gets damaged 25% of its health it gives currency.

Untested, but assuming you want to keep making the part take damage until its dead and award coins when 25% of the health is lost, something like this should do it. You’ll definitely need to adapt this logic to your specific implementation.

local ThingToDamage = ??? --Whatever you're trying to damage
local TotalHealth = ??? --The total health of the thing you're trying to damage
local AmountToDamage = ???--The amount of damage you want to deal
local SectorFactor = 0.25 --How often you want to get coins
local CoinReward = 20 -- How Many coins you want to get per sector

repeat -- Will keep looping until the thing is dead
    local OldHealth = ThingToDamage.Health.Value -- Assuming you have a health value
    local OldHealthSector = math.floor((OldHealth/TotalHealth)/SectorFactor)
    if OldHealthSector == 4 then
        OldHealthSector = 3 -- We dont want to give coins immediately for doing one damage
    end
    
    ThingToDamage.Health.Value -= AmountToDamage -- Damage thing

    local NewHealth = ThingToDamage.Health.Value
    local NewHealthSector = math.floor((NewHealth/TotalHealth)/SectorFactor)

    local CoinFactor = (OldHealthSector - NewHealthSector)
    if CoinFactor > 0 then
        local CoinsToGive = CoinFactor * CoinReward
        --TODO: Increase player's coins by CoinsToGive
    end
    task.wait(1)
until NewHealth <= 0 -- Stop once health is <= 0

To explain what Health sectors are, we divide the current health percent by the percent of how far the health needs to dip before the player gets coins. In the case of .25, we have a total of 4 sectors. By comparing the starting sector to the end sector, we can see how many times the health dropped by 25%, then multiply that number by the amount of coins to give.

2 Likes