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
print("Activated")
-- 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)
For some reason, when I join it only prints “Activated” twice and won’t print anymore.
Does anyone know what’s the problem?
In any case, Thank you for your time
Could be wrong but I don’t think GetPropertyChangedSignal will fire for changes affected by physics - e.g. player movement as this will take place each frame anyway. Use RunService.RenderStepped or equivalent instead.
Have you tried debugging, checking if the if statements are satisfied?
if yPos <= lastHeightCache[playerId] then
return -- Escape function
end
Make sure that the above statement isn’t met or it will return to the function it was called from. Just have a print before the return for debugging.