now I don’t 100% encourage using AI instead of learning scripting but just out of curiosity i’ve tried making a fall damage script with the AI, Weirdly enough everything works fine but it doesn’t change the player’s health nor print out any errors
local player = game.Players.LocalPlayer
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local minHeight = 10
humanoid.StateChanged:Connect(function(oldState, newState)
if oldState == Enum.HumanoidStateType.Freefall then
local fallStart = tick()
local fallEnd = tick()
humanoid.StateChanged:Connect(function(oldState, newState)
if newState ~= Enum.HumanoidStateType.Freefall then
fallEnd = tick()
local fallDuration = fallEnd - fallStart
local fallHeight = character.HumanoidRootPart.Position.Y
print("fallHeight: " .. fallHeight)
print("minHeight: " .. minHeight)
if fallHeight < minHeight then
local fallDamage = (fallHeight - minHeight) * fallDuration * 10
if fallDamage > 0 then
humanoid.Health = humanoid.Health - fallDamage
print("got fall damage")
end
end
end
end)
end
end)
And again, I do not encourage using AI over learning to code.
Locally modifying the user’s health (from a LocalScript) will not make it known to the server.
The server itself needs to perform the property change on the player’s health, so it replicates.
Try making this a server script instead, it should work in the same way.
Side note as well, but every time your initial StateChanged event fires (the outer event connection) you’ll end up creating another inner StateChanged event listener each time, this will cause memory to leak as those event connections do not get cleaned up until the script instance gets destroyed. I would advise using just one state handler instead.
ChatGPT feeds on existing text. This script looks atrocious, and I don’t blame the AI for it.
You simply need one StateChanged event, and not one inside of another, which creates memory leak. Also, as Sharpie mentioned, this must be done in the Server in order to replicate, so you can wrap that in a PlayerAdded event, or use Remotes.
Additionally, to calculate times, it’s 100% recommended to use os.clock() which returns a very high-precision CPU time used by Lua. tick() is merely the same as os.time() if I’m not wrong, which only returns seconds since UNIX.