how am i supposed to put this into my current script?
ive pasted this under the original script and sometimes the money rewarded becomes a negative value
Off the top of my head, the only way the value would become negative is if the Humanoid’s health is below zero, since that would create a negative percentage.
You’ll probably want to add a guard clause to make sure the Humanoid is alive, so add a
local currentHp = Humanoid.Health
if currentHp < 0 then return end
If that doesn’t work then try printing out each of the values to see where it is going wrong.
it seems to be working now, but it seems like when killing the npc its rewarding you for the amount of damage you dealt to the enemy and not the amount of health the npc had left
it also seems to be giving me negative values when using high firerate weapons
I have an idea for your game development, i came accross a problem with this issue and i haven’t found a solution for it. After thinking for a while, i manage to do it by myself without anyone’s help.
Here’s an example code with info and details in it:
local EnemyTarget = game.Workspace.Dummy -- Example Enemy
local GunDamage = 5 -- This is your gun damage
-- Let's say the health is 100 or the max health is 100
-- And then get the Gun Damage value, example is 5. (Per Hit)
-- The first method is if the enemy health is lower than the damage.
if EnemyTarget.Humanoid.Health < GunDamage then -- if 100 is less than 5 then do stuff.
-- We subtract the enemy's health and the gun damage : 5 - 4 (Example Enemy's Health is down to 4)
local Recalculate = GunDamage - EnemyTarget.Humanoid.Health -- 5 - 4 = 1
-- Now the enemy should be take damage upon hit with the exact damage which is 5.
EnemyTarget.Humanoid:TakeDamage(GunDamage)
-- Now we should earn the right money, because 5 damage per hit depends on the enemy's health.
game.Players.Pul129PG.leaderstats.Cash.Value += GunDamage - Recalculate -- We earn 5 - 1 = 4 cash upon killing it fully!
-- Now for the 2nd method. Different way of damaging the enemy with same cash, it means Damage = Cash. (Sorry for bad English)
elseif EnemyTarget.Humanoid.Health >= GunDamage then -- if 100 > 5 then do stuff.
-- Now the enemy should be take damage upon hit with the exact damage which is 5.
EnemyTarget.Humanoid:TakeDamage(GunDamage)
-- Now we earn exact cash with same damage which is 5.
game.Players.Pul129PG.leaderstats.Cash.Value += GunDamage
end
And now if you need help converting this to your script, just ask me and i’m here to help right away!
If you need more info about this example, here’s how…
Method 1:
-- The first method is used to check whether the enemies health is either lower than the damage value, for example: The enemy's health is 4 and the current gun's damage is 5. We cannot let it earn 5 cash, but instead give him the cash by 4 (Subtracting).
if EnemyTarget.Humanoid.Health < GunDamage then
end
Method 2:
-- The 2nd method is used if the enemy's health is more than or equal to the Gun's Damage value, it means the damage is 5 and the enemy's health is more than that (100 example), and also if the enemy's health is 5 but the damage is also 5, we earn the exact damage. That means overall the 2nd method is the exact damage.
--[[else]]if EnemyTarget.Humanoid.Health >= GunDamage then
end
What’s the difference between the two methods?
-
The first method is only used to earn exact enemy’s health cash.
(Example: Enemy’s Health is 4, but the damage is 5. We earn 4 cash after hit.) -
The 2nd method is only used to earn the exact cash with same damage.
(Example: Enemy’s Health is 35, but the damage is 5. We earn 5 cash after hit.)
Overall both of them are used for Tower Defense Game in my opinion.
Notice, this can be used anywhere. Not only in TDG
Let me know if i miss anything.