Now this is different from the last one I made. This is my script:
unit = 0.5 --Seconds damageperunit = 1 damage = 0 game.Players.PlayerAdded:connect(function(plyr) plyr.CharacterAdded:connect(function(char) local h = char:FindFirstChild("Humanoid") local hrp = char:FindFirstChild("HumanoidRootPart") while wait(unit) do if hrp.Velocity.Y < -1 then damage = damage+damageperunit else h:TakeDamage(damage) damage = 0 end print(damage, hrp.Velocity.Y) --Print how much damage you will take and how fast you're moving. end end) end)
As you know this script is a little glitched. If you can please fix this! Well I made a second version of this:
unit = 1 damageperunitmult = 1.5 --Change this to how much damage damage = 0 game.Players.PlayerAdded:connect(function(plyr) plyr.CharacterAdded:connect(function(char) local h = char:FindFirstChild("Humanoid") local hrp = char:FindFirstChild("HumanoidRootPart") if h then h.FreeFalling:connect(function() while wait(unit) do local speed = hrp.Velocity.Y if speed < 0 then damage = (speed*-1)+damageperunitmult elseif speed > 0 then h:TakeDamage(damage) damage = 0 end print(damage.." Damage and falling at "..speed) end end) end end)
Now they take 40 damage for just jumping! But some times when they fall from a tremendous height they take no damage. What is happening is that when they jump it takes less than a second so the "speed" variable doesn't have time to change. So the speed variable stays the same until one second passes.
I edited the script a little and I got this. Now they take 20 damage for walking and 40 for going up stairs. They get a lot more damage when they jump. Any help would be appreciated!
I asked this question before here. There was not much help but I edited the script and the edited script needs help!
I made a 3rd one! This one works better but I can't find the math for it. I'm trying to find the perfect amount of fall damage by Dividing. I divide it by 15 but It doesn't seem to do enough damage in some parts but seem to do too much damage at other parts.
unit = 0.5 --Seconds damageperunit = 1 damage = 0 game.Players.PlayerAdded:connect(function(plyr) plyr.CharacterAdded:connect(function(char) local h = char:FindFirstChild("Humanoid") local hrp = char:FindFirstChild("HumanoidRootPart") while wait(unit) do if math.floor(hrp.Velocity.y * 10^5) < 0 then damage = damage+((hrp.Velocity.Y*-1)/20) else h:TakeDamage(damage) damage = 0 end print(damage.." Damage and falling at "..math.floor(hrp.Velocity.Y*-10^5)) --Print how much damage you will take and how fast you're moving. end end) end)
The following is pseudo code(less pseudo code more "I deleted the exact answer") for a script that I made that works. IF you need more help I will offer some real code, but the point of this site isn't to give you all the answers.
damageperunitmult -- damage per amount of time/velocity of the fall timethreshold -- the amount of time the player must fall before damage is dealt speedthreshold-- the speed in the Y axis the player must be falling at to receive damage falling = false -- whether or not the player is falling local speed = 0 -- variable for velocity in the Y axis local time1 = 0 -- amount of time falling humanoid freefalls falling set to true while falling do add an increment to time get the current speed of the humanoid wait(0.04) --[[ You could bind this to RenderStepped, but I didn't think that was necessary. That might work better though, so you should try it.--]] end end on humanoid stopped freefalling do falling set to false --[[you can do one of three things here(that I can think of anyway). you could say "if time > timethreshold then" or "if speed > speedthreshold then" or combine both of them in some way. If the the "if" statement is passed then you can apply damage with your damageperunitmult * time or damageperunit * speed or some combination of both(or something completely different. This is just what I did). --]] end
Sorry about the wonky indentation. I'm not super literate in whatever text formatting this uses. Anyway, if it doesn't make sense feel free to ask questions, and if you really need it I can post some real code.
Edit: Figured out the indentation problem. If you don't add a new line(enter/shift+enter) a line that runs over the ends and starts a new line by itself will not respond to a space or a tab at the beginning of the automatically created line. Although what shows up on the post isn't what I see in the text editor, so I guess I'm just going to have a really hard to read post.
There are already many scripts out there that can do this, (however walking too fast/flying is a problem)
[code]bin = script.Parent humanoid = bin:FindFirstChild(“Humanoid”) ll = bin:FindFirstChild(“Left Leg”) rl = bin:FindFirstChild(“Right Leg”) la = bin:FindFirstChild(“Left Arm”) ra = bin:FindFirstChild(“Right Arm”) h = bin:FindFirstChild(“Head”) t = bin:FindFirstChild(“Torso”) ll.CanCollide = true la.CanCollide = true ra.CanCollide = true rl.CanCollide = true h.CanCollide = true t.CanCollide = true ll.Elasticity = 0 la.Elasticity = 0 ra.Elasticity = 0 rl.Elasticity = 0 h.Elasticity = 0 t.Elasticity = 0
function HitGround(part) local blah = bin:findFirstChild(“AlreadyHit”) if t.Velocity.y<-60 and part.Name~=“Pillow” and blah==nil then wee = Instance.new(“IntValue”) wee.Name = “AlreadyHit” wee.Parent = bin humanoid.Health = humanoid.Health + t.Velocity.y/3 wait(1) wee:Remove() end end
ll.Touched:connect(HitGround) rl.Touched:connect(HitGround) la.Touched:connect(HitGround) ra.Touched:connect(HitGround) h.Touched:connect(HitGround)[/code]
When I have done fall damage scripts I have usually used a circular buffer where I save the velocities of the last N frames.
This is reliable because with .Touched events, the event might come too late (and the velocity isnt the same as it was during impact) and additionally if you only check the velocity, the character will get hurt even if you just slightly touch a part when falling even if it doesnt even slow you down at all.
The buffer allows me to find the change in velocity within a short time period, so it basically causes damage whenever there is high acceleration such as when hitting the floor after a fall and quickly stopping.