Glitches with fall damage script

Hi developers, so I’m trying to create a fall damage script, everything works, but after I die the script won’t work anymore, does anybody know how to fix it?

It’s a LocalScript located inside the StarterPlayerScripts:

local Players = game:GetService("Players")

local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

local CanStart = true
local LastYPostition = nil
local FallDistance = nil

while wait() do
	if CanStart then
		CanStart = false
		repeat wait() until Humanoid.FloorMaterial == Enum.Material.Air
		
		wait(0.1)
		
		LastYPostition = Character.PrimaryPart.Position.Y
	end
	
	repeat wait() until Humanoid.FloorMaterial ~= Enum.Material.Air
	
	if LastYPostition ~= nil then
		FallDistance = (LastYPostition - Character.PrimaryPart.Position.Y)
	end
	
	if FallDistance ~= nil then
		if FallDistance <= math.huge and FallDistance >= 15 then
			Humanoid:TakeDamage(FallDistance)
		end
	end
	
	CanStart = true
end

print("ended")

Edit: And It’s not printing “ended”

1 Like

Use a server sided script instead, Player.PlayerAdded and then player.CharacterAdded
The reason it’s not working is probably because you havent put the script inside of startercharacterscripts

1 Like

Scripts inside the StarterPlayerScripts folder are only executed for when the player instance is added, which only occurs once in a single session. Scripts inside the StarterCharacterScripts folder are executed each time the player’s character model is added, which can occur many times across a single session.

2 Likes

It doesn’t write “ended” because you placed it under an infinite loop! :sweat_smile: