Scripting Help Needed

  1. What do you want to achieve?

Hello, I would like to achieve a working fall damage mechanism within my game.

  1. What is the issue?

I have followed a tutorial and ended up with this code:

local script = game.workspace.LocalPlayer

local character = player.Character

local humanoid = character:WaitForChild(‘Humanoid’)

local head = character:WaitForChild(‘Head’)

local.base = game.Workspace.Baseplate

local fall = false

local value = nil

local value2 = nil

local value3 = nil

humanoid.StateChanged:connect(function(state))
if state == Enum.HumanoidStateJumping then

	fall = true
	
	value = head.CFrame.Y - base.CFrame.Y
	
elseif state == Enum.HumanoidStateType.Running or state == Enum.HumanoidStateType.RunningNoPhysics then
	
	
	value = head.CFrame.Y - base.CFrame.Y
	
	elseifstate == Enum.HumanoidStateType.Freefall then
		
		fall = true
		
		elseifstate == Enum.HumanoidStateType.Landed then
			
			if fall == true then
				
				value2 = head.CFrame.Y - base.CFrame.Y
				
				value3 = value - value2
				
				if value3 > 10 then
					
					fall = false
					
					value3 = nil
					
					humanoid: TakeDamage(30)
				end
			end

end)

  1. What solutions have you tried so far?
    I have tried changing value3 to a lower number but it doesn’t seem to work. What have I done wrong? P.S this is not my own script as I am a complete beginner on scripting. Have I executed it wrong? I have placed it in StarterGUI folder

Based on the code example, it looks like you made a few typos and a couple of errors.

LocalPlayer is in Players, rather than workspace.

You added a dot between local and base on this line.

This should be Enum.HumanoidStateType.Jumping

All of the mentions of CFrame also need to be changed to Position.

If we go ahead and fix these things, we will see that it works!

And here’s the code after I fixed it up:

local player = game.Players.LocalPlayer
repeat wait() until player.Character ~= nil
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")
local head = character:WaitForChild("Head")
local base = game.Workspace.Baseplate
local fall = false
local value = nil
local value2 = nil
local value3 = nil

humanoid.StateChanged:Connect(function(state)
if state == Enum.HumanoidStateType.Jumping then
	fall = true
	value = head.Position.Y - base.Position.Y
elseif state == Enum.HumanoidStateType.Running or state == Enum.HumanoidStateType.RunningNoPhysics then
	value = head.Position.Y - base.Position.Y
elseif state == Enum.HumanoidStateType.Freefall then
	fall = true
elseif state == Enum.HumanoidStateType.Landed then
	if fall == true then
		value2 = head.Position.Y - base.Position.Y
		if value ~= nil and value2 ~= nil then
			value3 = value - value2
		else
			value3 = 0
		end
			if value3 > 10 then
				fall = false
				value3 = nil
				humanoid: TakeDamage(30)
			end
		end
	end
end)
3 Likes

Thanks a lot! Has CFrame been renamed or removed? Why is it base.position?

1 Like

Hmm, it’s weird because I applied your script to the game, first by manually typing it and then by copying and pasting and neither worked. Where did you place the script?

1 Like

Also would you make this a local or a server script?

1 Like

CFrame has not been removed or changed, that just wasn’t the correct use of it.

I put the LocalScript in StarterPlayerScripts.

2 Likes

I’ve put it in starterplayerscripts but it still doesn’t work for me, maybe it is the place I am falling from?

it works! I put it into character scripts also and it worked for me

I think you must move the damage block from ifs to another if that is if the first if is not true

Here’s the place I tested on.
Fall Damage.rbxl (16.4 KB)

It only works if I fall on the baseplate. Not on other blocks

That is because the variable “base” in your script is set to baseplate. I thought it was intentional. There’s still a few free models you can use that will accomplish the same thing.

R15: https://www.roblox.com/library/2048574817/R15-Fall-Damage
R6: https://www.roblox.com/library/154392509/Fall-damage-Script

Thanks, the creator seems to have deleted the baseplate though. I will need to find what the ground is. It was made using terrain tools I’m pretty sure.

Isn’t easier to make “while true do” and one if that is testing if player state is free fall setting fall to true and writing somewhere the actual height(but this to another if testing fall =false) and “else if fall ==true” and to this counting the damage and setting fall to false

The R6 link seems to be doing just that. I’m trying to manually write it to get used to writing code.

And you can’t count fall damage on 1 line because what if there is some hole and I will start falling next to it but land in it so I must save start of fall an from this minus land of fall height position