Question on Magnitude

Recently,I tried to make a script that the nearer you are with the object,the greater damage you will get.
However,I was stuck on the magnitude part,how can I make the variables become smaller,when the magnitude is larger

Reference:
E.g.
Player1 gets 5 damage when it is far away the object
Player2 gets 90 damage when it is close to the object

Humanoid.Health -= ?

Assuming you already have the code set up for the magnitude, you could do something like this

local distance = --Your magnitude distance code
local damage = 90 - math.round(distance)
local damageCalc = math.max(damage,5)

Humanoid:TakeDamage(damageCalc)

Where 90 is the maximum damage you want it to deal when you’re completely close to it and 5 being the minimum damage. This uses a relatively simple method of doing it where the damage is decreased by 1 per stud away. Should work for what is needed

2 Likes

Yep, also another way to do it with the similar concept is documented in Explosion | Roblox Creator Documentation.

The formula is roughly the same, yet different.

local distanceFactor = distance / explosion.BlastRadius -- get the distance as a value between 0 and 1
				distanceFactor = 1 - distanceFactor -- flip the amount, so that lower == closer == more damage
				humanoid:TakeDamage(maxDamage * distanceFactor) -- TakeDamage to respect ForceFields

Another method which is more customizable is to use a number sequence where you can adjust it more than just a linear function which I’m using right now. Message me if you are interested in this method, I gotta go to bed.

Edit: nvm here it is lol how my current explosion system works eval NS is the evaluate number sequence function taken from the
NumberSequence | Roblox Creator Documentation documentation.

local explosion: Explosion = explosions[index]
			local dropoffNS = explosionNumberSequences[index]
			local maxDamage = maxDamages[index]
			local maxRange = maxRanges[index]

explosion.Hit:Connect(function(part, distanceAway)
				local explosionPosition = explosion.Position
				local explosionDirection = part.Position - explosionPosition
				--local explosionRayResult = workspace:Raycast(explosionPosition, explosionDirection, explodeParams)
				--if explosionRayResult and explosionRayResult.Instance == part then
					local decimalPercentAway = distanceAway / maxRange
					decimalPercentAway = math.clamp(decimalPercentAway,0,1)
					local damagePercentage = evalNS(dropoffNS, decimalPercentAway)
					local damageToApply = damagePercentage * maxDamage

					ServerWorldModule.damageInstance(part, damageToApply)
				--end
			end)

Edit 2: if you want to get more math savvy you can use trend fitting to graph the numbers you want to a mathematical equation that describes the relationship like so:

3 Likes