How would I make the player take damage as he goes up?

I would like to make a script that damages the player as he goes up from a certain height and takes more damage from a lower height.

So basically, if the player is under 50 studs and he goes up, he takes 2 damage per second

But if the player is under 100 studs and he goes up, he takes 5 damage per second.

How would I go about that?

1 Like

try adding this script in StarterPlayer > StarterCharacterScripts

spawn(function()
	local Char = script.Parent
	local Hum = Char:FindFirstChild("Humanoid")
	local HRP = Char:FindFirstChild("HumanoidRootPart")
	
	while wait(1) do
		if Hum ~= nil then
			if HRP ~= nil then
				local CurPos = HRP.Position
				
				if CurPos.Y < 100 then
					Hum:TakeDamage(2)
				elseif CurPos.Y >= 100 then
					Hum:TakeDamage(5)
				end
			end
		end
	end
end)

i just tested that out

idk if thats what your trying to do…

1 Like

It kinda works, trying to make the player take damage only once he goes up though, so if he goes down or stays the same he doesn’t take damage but if he climbs up, he does.

oh so when climbing? (30 charrrsssss)

Yeah, exactly (30charrrrrrrrrrrrrrrrrssssss)

So, basically the lower the player goes, the more damage they take every second?

This is easy to do. All you need to do is check the players position on the Y axis. If that position’s Y value is under 50 studs then damage the player

Example;

Char = script.Parent

while true do
    wait(1)
    if Char.HumanoidRootPart.Position.Y <= -50 then
        Char.Humanoid:TakeDamage(2)
    elseif Char.HumanoidRootPart.Position.Y <= 100 then
        Char.Humanoid:TakeDamage(5)
    end
end

thats like what i did. but he said when the Y axis is changed only or go down of up

Can you please rephrase that?

This isn’t making much sense

like when the player’s position went up or down

He didn’t really say anything about going up.

i meant like when the player is climbing up or down

So when the player goes up or down? wouldn’t the player always be taking damage at that point?

thats what he said

Well, a loop wouldn’t work then. You could just have invisible non colideable parts that damages the player upon touching them.

That’s probably what I will do eventually.

HumanoidState.Climbing (30 chars)

Not really, they aren’t climbing physically, they are just going up just like climbing up stairs for example

Tossed this together, should work, haven’t tested it though. The cooldown is to make the damage per second.

local lastHeightCache = {}

game:GetService("Players").PlayerAdded:Connect(function(player)
    local playerId = player.UserId
    local character = player.Character or player.CharacterAdded:wait()
    local humanoid = character.Humanoid
    local head = character.Head
    lastHeightCache[playerId] = head.Position.Y
    head:GetPropertyChangedSignal("Position"):Connect(function()
        local yPos = head.Position.Y
        -- If they've not moved up:
        if yPos <= lastHeightCache[playerId] then
            return -- Escape function
        end
        -- If they have moved up:
        if cooldown == true then -- If a second has passed since last damage, start a cooldown
            coroutine.wrap(function()   
                cooldown = false
                wait(1)
                cooldown = true
            end)()
        elseif yPos <= 50 then
            humanoid:TakeDamage(2)
        elseif yPos <= 100 then
            humanoid:TakeDamage(5)
        end
        lastHeightCache[playerId] = yPos -- Update the last height
    end)
end)
1 Like

Hey, thank you for helping but for some reason I get an error:
" [Workspace.cqRoy.Script:8: Expected identifier when parsing expression, got ["

So the error is here:

    local lastHeightCache[playerId] = head.Position.Y

try replacing local playerId = player.UserId with local playerId = player.Name