Hello, I would like to achieve a working fall damage mechanism within my game.
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)
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)
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?
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.
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
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