[EDIT] - Changed script to work 100% server side, fixed grammar
[EDIT 2] - Changed how falling is detected
Recently whilst looking on the internet for good, robust fall damage tutorials I realized that most if not all only kill or give a predefined set of damage that is not dependent on the height you fell from. So that’s what we’re going to be doing here!
This is available as an Experience to test it out
Setup
In ServerScriptService add a blank script, this is going to be our Fall Damage Controller (FDC) script
In StarterCharacterScripts add a local script, this is going to be our Fall Detector (FD) script
For testing purposes I will also add a TrussPart in the Workspace
Defining Variables
Before we start we also need to define the MinVelocity and the MaxVelocity. MinVelocity is the minimum required Velocity/Speed to trigger fall damage and MaxVelocity is the required Velocity to kill a player.
You can either use variables or attributes. For this tutorial we will be using the latter.
To add attributes to a script
-
Select the FDC script in ServerScriptService and click Add Attribute in properties
-
In the pop up choose the type as number and Name as
MinVelocity
-
Repeat again for
MaxVelocity
Set these values to whatever you want. A MinVelocity of 25 is recommended
Detecting a Fall
Now that we have our MinVelocity and MaxVelocity we can get to coding!
In our FDC script we need to define the Attributes we set eariler. If your using variables, replace these with normal variables
local MaxVelocity = script:GetAttribute("MaxVelocity")
local MinVelocity = script:GetAttribute("MinVelocity")
To detect our player falling we can wrap all of our code in a CharacterAdded
event
game.Players.PlayerAdded:Connect(function(Plr)
Plr.CharacterAdded:Connect(function(Char)
-- All New Code goes in here
end)
end)
Inside our event we want to detect if the Character’s “State” has changed
local Humanoid = Char.Humanoid
local HumanoidRootPart = Char.HumanoidRootPart
Humanoid.StateChanged:Connect(function()
end)
When it’s changed we need to get our HumanoidRootPart’s Velocity, and invert it so it’s a positive number
local PlrVelocity = HumanoidRootPart.Velocity.Y
PlrVelocity *= -1
After we’ve gotten the Player’s velocity we’re going to do two checks
- Player Velocity is greater than MaxVelocity (Yes - Kill them | No - Continue)
- Player Velocity is greater than MinVelocity (Yes - Take Damage | No - Do Nothing)
if PlrVelocity > MaxVelocity then
Humanoid.Health = 0
elseif PlrVelocity > MinVelocity then
Humanoid.Health -= PlrVelocity / 2
end
Your full script should look like this
local MaxVelocity = script:GetAttribute("MaxVelocity")
local MinVelocity = script:GetAttribute("MinVelocity")
game.Players.PlayerAdded:Connect(function(Plr)
Plr.CharacterAdded:Connect(function(Char)
local Humanoid = Char:WaitForChild("Humanoid")
local HumanoidRootPart = Char:WaitForChild("HumanoidRootPart")
Humanoid.StateChanged:Connect(function(OldState, NewState)
local PlrVelocity = HumanoidRootPart.Velocity.Y
PlrVelocity *= -1
if PlrVelocity > MaxVelocity then
Humanoid.Health = 0
elseif PlrVelocity > MinVelocity then
Humanoid.Health -= PlrVelocity / 2
end
end)
end)
end)
Print the Player’s velocity and change values as need
Conclusion
That’s the end of the tutorial! If anything doesn’t work try and debug it if you can, if you can’t post in the comments!
This is my first ever post so constructive criticism is appreciated!