My fall damage script is not breaking trough the loop

(Alright, This time, I will post the code)

I am working on a system that allows the player to glide and avoid fall damage, but for some reason, the gliding script is making the fall damage script not break out of the loop

Here is the fall damage script
-- Objects
local settingsDir = script.Settings

function getSetting (name)
	return settingsDir and settingsDir:FindFirstChild(name) and settingsDir[name].Value
end

-- Variables
local damageHeight = getSetting("Damaging height") or 14 -- The height at which the player will start getting damaged at
local lethalHeight = getSetting("Lethal height") or 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)
And this is what I have of the gliding script
local player = game.Players.LocalPlayer

local RunService = game:GetService("RunService")
local ContextActionService = game:GetService("ContextActionService")

local char = script.Parent

falling = false

char.Humanoid.StateChanged:Connect(function(_,state)
	if state == Enum.HumanoidStateType.Freefall then
		falling = true
		wait()
	elseif state ~= Enum.HumanoidStateType.Freefall then
		falling = false
		wait()
	end
end)

local function Glide(actionName, inputState)
	if inputState == Enum.UserInputState.Begin and falling then
		script.Parent.Falldamage.TakeDamage.Value = false
		local lowgrav = Instance.new("BodyVelocity")
		lowgrav.Parent = char.Head
		lowgrav.MaxForce = Vector3.new(0, 100000, 0)
		lowgrav.P = 10000
		lowgrav.Velocity = Vector3.new(0, -10, 0)
		lowgrav.Name = "LowGravity"

	elseif falling == false or inputState == Enum.UserInputState.End or inputState == Enum.UserInputState.Cancel or inputState == Enum.UserInputState.None  then
		local lowgrab = char.Head:FindFirstChildOfClass("BodyVelocity")

		if lowgrab then
			char.Head.LowGravity:Remove()
		end
		wait(.1)
		script.Parent.Falldamage.TakeDamage.Value = true

	end
end

game.ContextActionService:BindAction("Q", Glide, true, Enum.KeyCode.Q, Enum.KeyCode.ButtonA)

Both of these scripts go under “StarterCharacterScripts” inside “StarterPlayer” and each is called “FallDamage” and “Gliding” respectfully, And the fall damage script must have a BoolValue instance that is active and called “TakeDamage”

I have been trying absolutely everything to try and fix this, but the things keep breaking.

Any help?

You can try using bool value to set a true or false state in the scripts. And makes them check if the player is currently falling or not.

2 Likes

Hmm. It seems slightly complicated. What if, when a player joins, you assign a false bool value within the player called “Gliding”. In the glide script, make this true when they’re gliding and false again when they stop. In the fall damage script simply make sure the value is false before removing health.

Edit: Whoops, @Osrock_626 replied while I was typing.

2 Likes

XD bro, but we are in this to help this boi.
Yes @Officiall_Studios and I are in agreement, a bool can force a script to not break a loop, like a floodgate or a switch.

2 Likes

I created something like that, A bool value called “TakeDamage” to tell when the player should take damage or not, but it still does not work.

Did you make sure that script turn on and off the bool when needed to?

1 Like

The bool value works, It’s that for some reason the loop never breaks and never tells when the player stopped falling

Maybe a return could help here, like return this if instance is true.

1 Like

I cannot find where I could return the function and it would actually work

It is still broken, no matter what!

I think I may have a solution, but I’m can’t be too sure so I have a few proposals.

  1. Taking a microscopic look at the script, I can see that:
if fell >= lethalHeight then
   humanoid.Health = 0
elseif fell >= damageHeight then
   humanoid.Health = humanoid.Health - math.floor(fell)
end

On the esleif, it’s registering that a value that is greater than or equal to the damage height, but it could also be registering the lethal height too, meaning both the if and the elseif are firing at the same time, now yeah I don’t think that would be the case, since killing the player would, you know, kill him, but it could be an issue. Perhaps we can try to set a range for the damage height instead of saying a greater than or equal to operator.

  1. The wait(3) is weird, is there a spawn point or does the player spawn in the sky, because the player shouldn’t take damage from only spawning. (Some examples are Jailbreak and Wild West)?
    Also, is the a :GetChangedProperty available for the humanoid? Because the function isn’t going to able to register that the humanoid is falling since I don’t think that humanoid.FreeFalling is enough to trigger. An example of such is a tween service, like:
Tween.Completed:Connect(function(playbackState)
   if playbackState == Enum.PlaybackState.Completed then
      --Notice that I had to use an if statement to complete the function, now yeah I know it's weird, but unfortunately this is the only way to make I work.
   end
end)

Hope these help, but I can’t be too sure.

1 Like

I cannot see where the tweenfunction would fit of second half in point 2, The values are different in point 1

1.Yeah they are, in: local fell = headHeight - root.Position.Y it is literally a number called the Y axis.

The tween was an example.

1 Like
  1. What is wrong about that being called a Y axis?

Yes. I understood the tween was an example, but I cannot find where it could be in my script

Nothing, it’s a number though so it could mess up the script. Anyway I just thought about some solutions but looks like it didn’t work, F :sad:

1 Like

I appreciate your attempts at helping me tough, Next time maybe someone who can actually test my script could help me.

1 Like

Your time to shine, Osrock did not manage to solve my problem, Now it is up to you!

Alright. Let me write a few things. I’ll have a script or two in just a second.

1 Like

First off, I’m going to start by assuming you have a leaderstat. If this is wrong, correct me and I’ll write a full script. When assigning the leader stats folder, add this line of code below it:

local gliding = Instance.new(“BoolValue”, player) —if your variable for player is different replace it here.
gliding.Name = “Gliding”

Now, this is fairly self explanatory. We are just adding a value to the player which we will toggle and check for later.

Now, right after you create the LowGravity, within the gliding script, add:

local gliding = player:FindFirstChild(“Gliding”)
gliding.Value = true

This should make the value true whenever the player starts gliding. Of course, we have to make it false one it is done. After your line to :Remove() the LowGravity, add this:

local gliding = player:FindFirstChild(“Gliding”)
gliding.Value = false

This is almost the exact same as the last line you added, but this time you are setting the value to false. Right now, it should set the value to true and false when it should. Time to make the fall damage pay attention to this.

To keep this organized, let’s declare the gliding variable right after the fell variable.

local gliding = plr:FindFirstChild(“Gliding”)

if gliding.Value == true then return end

Here we are just declaring the gliding variable again. After that we check if its value is true. If it is, we stop the function.

That should work, fingers crossed! I apologize if it doesn’t, or there are typos in the code, I wrote this all on mobile. Hope this helped!

1 Like

I do not have any leaderstats, I usually add a folder that is NOT named Leaderstats just because I love making my GUI customized.

Ah. Then you are going to need a small script in ServerScriptService.

game.Players.PlayerAdded:Connect(function(player)
  local gliding = Instance.new(”BoolValue”, player)
   gliding.Name = “Gliding”
end)
1 Like