Player Randomly Takes Fall Damage When Being Teleported?

I am trying to create a basic fall damage system to damage the player if the humanoid lands with a Y velocity (abs) of 30 or greater. Here is the code I am using to achieve this:

hum.StateChanged:Connect(function(old,new)
	if new==Enum.HumanoidStateType.Landed and old==Enum.HumanoidStateType.Freefall then
		if math.abs(root.AssemblyLinearVelocity.Y)>30 then
			hum.Health-=math.clamp(math.abs(root.AssemblyLinearVelocity.Y),1,math.huge)
		end
	end
end)

This code works perfectly fine, except when teleporting the player from one location to another. When teleporting, the player will occasionally take a random amount fall damage (teleporting between same locations always).

I’ve tried countless solutions, such as checking the Y coordinate difference between when the Humanoid enters Freefall and when it lands, or setting the velocity of the HumanoidRootPart to 0 after teleportation (though I don’t think this does anything), but nothing has worked so far. The forums have not been very helpful either, as most posts relating to fall damage are requests on how to make the system, and none address the issue I am experiencing.

Any help is appreciated!

you could just increase the minimum velocity, 30 is actually pretty low

also, you don’t need to check if newstate == Enum.HumanoidStateType.Landed since oldstate == Enum.HumanoidStateType.FreeFall does the same thing and is more reliable!

hum.StateChanged:Connect(function(last)
	if last == Enum.HumanoidStateType.Freefall 
	and math.abs(root.AssemblyLinearVelocity.Y) > 128
	then
		hum.Health -= math.clamp(math.abs(root.AssemblyLinearVelocity.Y) / 2, 1, math.huge)
	end
end)

hope this helps!

1 Like

30 is not that low, it’s ok for some games. but I agree that detecting last state is better.
Maybe the teleport script is the problem?

for fall damage, 30 is low, i checked by printing in a loop and the velocity reached 30 pretty quick, but yea the teleport script could also be the problem, that should be checked out as well

what was the max velocity?
I was just guessing since the velocity is studs per second I think.

i didn’t check the maximum velocity, but with default gravity from about 40-50 studs it reached around 180 by the time i fell all the way down, i believe the velocity doesn’t have a limit, if it does it’s definitely much higher than 180

Yeah, I forgot that part, roblox is a bit ‘poorly scripted’ since they didnt add max speed :skull:
(because you can noclip throught parts)
Then I guess 50-80 is a decent minimum fall dmg velocity :person_shrugging:

i’d say around 90 minimum is ideal, 50 is still very low, but of course it depends on the game and the environment

1 Like

Thanks for the feedback!

I use 30 because the distance between two floors in the environment is 18 studs, and I want the fall from one floor to leave the player with not a lot of health.

As for the teleport, it’s 1 line called on the server:

plr.Character.HumanoidRootPart.CFrame=spawn.CFrame*CFrame.new(0,3.4,0)

if that’s the case, then stick with 30, i still recommend using my code for it since it’s more reliable though

as for the teleport, try using this instead:

root.Position = spawn.CFrame.Position + Vector3.new(0,3.4,0)

also, please add a print(math.abs(root.AssemblyLinearVelocity.Y)) under the part where it checks the humanoidstatetype, and let me know what it prints after the teleport

The player is still randomly taking fall damage. Out of about 4 runs, I got it to happen once and it printed -53.13

I guess using CollectionService to tag or untag while such humanoid state is happening?

sorry for the late response, i think it’s best if you assign a boolean attribute to the character, and check it while being teleported, and uncheck it once the player hits the ground, using SetAttribute() and GetAttribute()

i can’t provide examples at this time, i’ll try later

Sorry for replying late, but this solution is extremely inconsistent and, for whatever reason, doesn’t work. If I add a simple task.wait(.1) after teleportation before setting the attribute to false, it works in studio but does not in game (my guess is that fact that in studio the server and client are both my computer so there is no delay). If I add a connection to when the state of the humanoid changes from FreeFall, for whatever reason that also does not trigger in game, but works fine in studio.

Never mind can’t just guess this one and I can’t test atm.

You can turn the humanoid’s platformstand to true and set it to false once they’ve reached the location… just throwing some things out there without much thought but hopefully it helps