Everything looks great. But if you want a popular game another way to improve it is add effects like dirt reflecting off of the ground or other materials. Also gui showing and displaying how much damage was taken.
2 things:
-Health does not replicate, you should be firing a remote event whenever someone takes fall damage so that the server can damage the humanoid.
-From my experience, the character’s velocity property is not very precise and sometimes falling from the same height could deal different fall damages, so the best option would be calculating how much time the character spent on freefall state.
So… how i can use a Remote Event to the humanoid?
I tried a few times, but i didn’t found a solution or maybe help about how to replicate Humanoid Health.
First, you need to store the Remote event can be accessed by client and server, replicated storage would be the best. Name it whatever you want, I’ll refer to it as FallDamage
On server sided script you write (put the script into ServerScriptService):
local FallDamageRemote = game:GetService("ReplicatedStorage").FallDamage
FallDamageRemote.OnServerEvent:Connect(function(player, damageAmount)
--[[first param is always player
you can add more params like I did with damage amount]]
local char = player.Character
if not char then return end
local humanoid = char:FindFirstChildWhichIsA("Humanoid")
humanoid:TakeDamage(damageAmount)
end)
and to fire the remote you write this on the client side:
local FallDamageRemote = game:GetService("ReplicatedStorage").FallDamage
FallDamageRemote:Fire(10) --does the function inside the script in server, damaging the player by the amount specified which is 10
Also be careful with Remote Events, anyone can fire them. Meaning that if exploiters wanted they could fire this event however they wanted, but as I get the player humanoid on server, it makes it impossible to damage other players than themselves.
If you don’t want to make them damage themselves then you can add more safety checks (such as checking player humanoidState and stuff on the server)
You don’t need to change the local script location, and it is better to keep it at StarterCharacterScripts
If you wanna check the state you should use humanoid:GetState(), also do not pass humanoid as a parameter, as exploiters can pass other players humanoids
Every time you do a connection (:Connect) it uses memory, it also does everything in the code many times if you do it multiple times. As such you should not try to generate a connection every time you wanna check something.
If you wanna switch the code to server side, you could just make the whole script on server side
Example of server sided code: (Credit to realism mod for the code)
-- Variables
local damageHeight = 14 -- The height at which the player will start getting damaged at
local lethalHeight = 26 -- The height at which the player will get killed
game:GetService("Players").PlayerAdded:Connect(function (plr)
plr.CharacterAdded:Connect(function (char)
local root = char:WaitForChild("HumanoidRootPart")
local humanoid = char:WaitForChild("Humanoid")
if humanoid and root then
local headHeight
wait(3) -- Prevent the player from dying on spawn
humanoid.FreeFalling:Connect(function (state)
if state then
headHeight = root.Position.Y
elseif not state and headHeight ~= nil then
pcall(function ()
local fell = headHeight - root.Position.Y
if fell >= lethalHeight then
humanoid.Health = 0
elseif fell >= damageHeight then
humanoid.Health = humanoid.Health - math.floor(fell)
end
end)
end
end)
end
end)
end)