Script triggers only twice?

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 :slightly_smiling_face:

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.

2 Likes

Hello, thank you for your answer but even when I tried

 while true do

It still didn’t work, so I doubt RunService.RenderStepped will do much of a difference.

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.

Hello and sorry for late response,
I did have a print when I put it in that part but again, it only printed twice.

I just woke up, so it may just be my being stupid, but is it got to do something with the end after cooldown = true

Uhh… Don’t think so, everything seems fine to me.

Alright. Well can’t you make an infinite function??

while wait(2) do
    functionHere()
end