Information
After around 2 days of work, including a DevForum post (that I found the solution to myself lol), I am releasing my Fall Damage script!
As you can see here in this video, you take damage when falling from at least 75 studs. (This can be changed to your likings in the script)
If you just want to add the script right away to your game, here is the link to the model:
Here is a game to test this in:
However, I don’t recommend doing this without reading the rest of this post.
Main Script Breakdown
This script has 2 other scripts inside it too. There are 2 LocalScripts, and a ModuleScript.
First, lets talk about the main script, and how it works.
local Character
repeat task.wait() until script.Parent:FindFirstChild("Humanoid")
Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local HRP = Character:WaitForChild("HumanoidRootPart")
local Values = require(script.Values)
local function CalculateDamage(Magnitude)
-- Calculates the amount of damage the player will take from the fall.
local afterminus100;
local totaldamage;
afterminus100 = Magnitude - Values.MinimumFallDamageStuds
if afterminus100 >= 0 then
totaldamage = Magnitude*Values.DamageMultiplication + 10
else
totaldamage = 0
end
return totaldamage
end
Humanoid.StateChanged:Connect(function(oldstate,newstate)
local magnitude;
local total;
if newstate == Enum.HumanoidStateType.Landed then -- Tells when the player landed
--print('landed')
Values.DontUpdateYet = true;
magnitude = (Values.LastStandingPos - HRP.Position).magnitude -- measures the y position of where they fell from, and the Y position they have after they touched the ground.
total = CalculateDamage(magnitude)
-- Here, the script checks if the total is greater than 0. If it is, then it damages the player and plays the sound.
if total > 0 then
Humanoid.Health = Humanoid.Health - total
script.DamageSound:Play()
end
Values.DontUpdateYet = false;
end
end)
The script starts off by doing a repeat loop until the Character is found.
The script then defines 3 variables, including the ModuleScript that will be talked about later in this post.
After those 3 variables are defined, a function named CalculateDamage is defined. A parameter is passed through when calling this function, which is the Magnitude.
Before I continue, for anyone unaware of what Magnitude is, it is the length of a vector.
You can read more about it here:
Now, lets get back to the script.
2 variables in this function are defined:
-
afterminus100 (don’t mind the name, the minimum studs when I began working on this script 2 days ago was going to be 100 studs, and I’m too lazy to fix it, so just don’t mind the name of this variable.)
-
totaldamage
afterminus100 will be used to determine how many extra studs they fell above 75 (or your number of studs).
totaldamage will be used later for the total amount of damage to do to the player’s Humanoid.
On the next line (line 16), a value is set for afterminus100. The value is set by the Magnitude subtracted by the Minimum amount of studs you set to take fall damage in the Values ModuleScript.
After this, the script checks if afterminus100’s value is greater or equal to 0. If it isn’t, totaldamage is set to 0.
If it is however, totaldamage’s value is set to the Magnitude’s value * the value you set for the damage multiplication in the Values ModuleScript. I set it at 0.05, however you can change it to whatever you would like it to be.
return totaldamage
After all of this, the totaldamage variable is returned, so whenever this function is called, it returns the total amount of damage the player will take from the fall.
Next up is detecting when the state of the Humanoid is changed.
This is done by using Humanoid.StateChanged
.
local magnitude;
local total;
if newstate == Enum.HumanoidStateType.Landed then -- Tells when the player landed
--print('landed')
Values.DontUpdateYet = true;
magnitude = (Values.LastStandingPos - HRP.Position).magnitude -- measures the y position of where they fell from, and the Y position they have after they touched the ground.
total = CalculateDamage(magnitude)
-- Here, the script checks if the total is greater than 0. If it is, then it damages the player and plays the sound.
if total > 0 then
Humanoid.Health = Humanoid.Health - total
script.DamageSound:Play()
end
Values.DontUpdateYet = false;
end
end)
This is what we will be looking at now.
After Humanoid.StateChanged
is connected to a function, we use the 2 parameters:
- oldstate
- newstate
These paramets easily allow us to identify the previous state of the Humanoid, and not go through a nightmare doing it. Thanks Roblox for once
Anyways, this function starts by defining 2 variables.
- magnitude
- total
The script then checks the value of the newstate parameter:
if newstate == Enum.HumanoidStateType.Landed then -- Tells when the player landed
If it equals
Enum.HumanoidStateType.Landed
Then, the script will know the player just landed, as this state lasts only for a brief amount of time as can be seen here:
A variable in the Values ModuleScript named “DontUpdateYet” has been set to false. What is this variable, and what will it do?
Well, as will be seen when we get into the second script, it will tell if it the LastStandingPos is able to be updated yet.
Why does this need to exist?
Because the LastStandingPos will update right before the fall CalculateDamage function runs, therefore the Magnitude that will be passed through will be like 0 or something, and that’s not what we want.
Next line, we get back to that HRP variable.
I usually type HRP instead of HumanoidRootPart in my scripts, as it is much easier to type and faster.
local lastHeight = Values.LastStandingPos.Y
local currentHeight = HRP.Position.Y
magnitude = lastHeight - currentHeight -- measures the y position of where they fell from, and the Y position they have after they touched the ground.
This is the line that calculates the magnitude. Nothing to mention here that I know of.
total = CalculateDamage(magnitude)
The total damage that the player will take is then calculated using the CalculateDamage function.
if total > 0 then
Humanoid.Health = Humanoid.Health - total
script.DamageSound:Play()
end
Values.DontUpdateYet = false;
The script then checks if the total is over 0. If it is, then the player takes the fall damage and then plays the fall damage sound. I will mention this sound again later in this post.
Anyways, at this point you should understand what is going on for the rest of this code snippet.
That is the end of this script.
Second Script Breakdown
This script is more straight forward, unlike the last one. I’m not going to explain anything here, however there are some comments in the script.
-- The script needs this to check the state to not update the Last Standing Position Variable.
local function getHumanoidState(h)
return h:GetState()
end
-- The states where the Last Standing Y Position variable will not be changed.
local States = {
[Enum.HumanoidStateType.Running] = true;
[Enum.HumanoidStateType.RunningNoPhysics] = true;
[Enum.HumanoidStateType.Climbing] = true;
[Enum.HumanoidStateType.Ragdoll] = true;
[Enum.HumanoidStateType.FallingDown] = true;
[Enum.HumanoidStateType.Flying] = true;
[Enum.HumanoidStateType.GettingUp] = true;
[Enum.HumanoidStateType.Dead] = true;
[Enum.HumanoidStateType.None] = true;
[Enum.HumanoidStateType.Physics] = true;
[Enum.HumanoidStateType.Swimming] = true;
[Enum.HumanoidStateType.Seated] = true;
[Enum.HumanoidStateType.PlatformStanding] = true;
}
local Character
repeat task.wait() until script.Parent.Parent:FindFirstChild("Humanoid")
Character = script.Parent.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local HRP = Character:WaitForChild("HumanoidRootPart")
local Values = require(script.Parent.Values)
while true do
local state = getHumanoidState(Humanoid)
--print(state)
if States[state] and not Values.DontUpdateYet then
Values.LastStandingPos = HRP.Position
--print(Values.LastStandingPos)
end
task.wait()
end
ModuleScript Breakdown
local module = {}
module.LastStandingPos = nil;
module.DontUpdateYet = false;
module.DamageMultiplication = 0.05
module.MinimumFallDamageStuds = 75
return module
This script is extremely straight forward, it simply stores the values that were explained earlier.
Fall Damage Sound
Due to Roblox’s recent audio privacy update, it is not possible for anyone to use the same ID, and as of now Roblox does not allow making audios public. (Thanks a lot Roblox)
Until Roblox allows people to make their audios public, the only way for me to share this audio with you is to put the file for it in this post, and for you to upload it yourself.
Here is the audio file:
Obviously, you can change it to your own sound, but if you want the same one as in the video shown at the beginning of this post, this is your only way of getting it.
*How to add this script to your game
Here is a model of the script:
Simply take the model, import it in from Toolbox, and put it in StarterPlayer → StarterCharacterScripts.
You can then change the settings I mentioned earlier in the Values Module.
Anything else?
I don’t have anything else to say. Enjoy the script!