I want to incorporate a bit of reality into my game; as my player falls, his or her momentum increases, and if he or she suddenly hits the ground, depending on the height from which he or she dropped, death may occur.

I've created a couple scripts for this. Basically, when any part of the player comes into contact with a brick known as "Ground," he or she will be considered to be no longer in FreeFalling, and therefore landed. When I tried this, however, the first of the following scripts errored, saying that the Character property is a nil value, which doesn't make sense to me. Could anyone tell me why this is?

The first script (the one that errored.) A LocalScript that is deposited into the player upon entering the game.

player = game.Players.LocalPlayer

function Landed(part)
	if part.Name == "Ground" then --I am ensuring that the part is specifically named "Ground"  because I don't want the player to die when falling into a pond or through a cloud.
		player.SpeedFolder.Landed.Value = true --The "landed" value is simply used to keep track of whether or not the player is falling. This value is deposited into the player prior to this very script.
	end
	return true
end

enabled = true
player.Character.Head.Touched:connect(function(part) --This is the script that continues to error; when I test this game, this script returns that the Character property is a nil value, which makes no sense to me.
	if enabled == true then
		enabled = false
		local a = Landed(part)
		repeat wait() until a == true
	end
	enabled = true
end)

player.Character.Torso.Touched:connect(function(part) --Applying this function to all of the other body parts in the player.
	if enabled == true then
		enabled = false
		local a = Landed(part)
		repeat wait() until a == true
	end
	enabled = true
end)

player.Character.LeftArm.Touched:connect(function(part)
	if enabled == true then
		enabled = false
		local a = Landed(part)
		repeat wait() until a == true
	end
	enabled = true
end)

player.Character.RightArm.Touched:connect(function(part)
	if enabled == true then
		enabled = false
		local a = Landed(part)
		repeat wait() until a == true
	end
	enabled = true
end)

player.Character.LeftLeg.Touched:connect(function(part)
	if enabled == true then
		enabled = false
		local a = Landed(part)
		repeat wait() until a == true
	end
	enabled = true
end)

player.Character.RightLeg.Touched:connect(function(part)
	if enabled == true then
		enabled = false
		local a = Landed(part)
		repeat wait() until a == true
	end
	enabled = true
end)

Another script that is placed in the player's PlayerScripts. This one handles the damage a player is supposed to take when he or she lands. Since the previous script continues to error, the player simply dies after the "t" value in this script reaches less than -20, which is far too long after the player has landed.

debounce = false --To ensure that multiple BodyVelocities are not added to the player.
game.Players.LocalPlayer.Character.Humanoid.FreeFalling:connect(function() --If the player is in FreeFalling, this script shall activate.
	print("IS FALLING")
	game.Players.LocalPlayer.SpeedFolder.Landed.Value = false --To say that the player is now no longer standing on a platform.
	local player = game.Players.LocalPlayer.Character
	if debounce == false then
		debounce = true
		local v1 = Instance.new("BodyVelocity", player.HumanoidRootPart) --Adding a bit of fall speed.
		v1.Name = "Fall"
		local t = 0
		repeat
			wait(.1)
			t = t - .2
			v1.Velocity = Vector3.new(0, t*25, 0) --As time passes, the player's falling speed increases.
		until game.Players.LocalPlayer.SpeedFolder.Landed.Value == true or t <= -20 --The t <= -20 statement is simply used in the event that something happens and the player is just continuously falling. Since the previous script errors and the Landed value never becomes true, the player simply dies when t is less than or equal to -20, far after his or her contact with the ground.
		v1:Destroy() --Destroy the BodyVelocity.
		if t < -4 then
			player.Humanoid.Health = player.Humanoid.Health + t*25 --The health a player loses depends on how fast he or she was traveling before impacting the ground.
		end
	end
	debounce = false
end)

Any help or recommendations is highly appreciated!

3 comments

0

The character may just not be loaded. Use 'repeat wait() until plr.Character' or 'local chr = plr.Character or plr.CharacterAdded:wait()'. Either option works.

0

It seems to me that you could use the fall damage plugin, and then just make a script that adds velocity the longer you fall.

0

2^ Char = player.Character or player.CharacterAdded:wait() is the best.