Drowning script not working

So basically I’m making a drowning script, but it just won’t work. No errors, just nothing happening.

game.Players.PlayerAdded:Connect(function(player)
    DrowningStats = Instance.new("IntValue")
    DrowningStats.Name = "DrowningStats"
    DrowningStats.Parent = player
end)
local region = Region3.new(Vector3.new(-29.5, 0.5, -45.5), Vector3.new(-79.625, 6.375, -87.5))
local RePart = Instance.new("Part")
RePart.Size = region.Size
RePart.Transparency = 0.5                           --this part didnt work
RePart.CanCollide = false
while true do
    wait(1)
    local partsInRegion = workspace:FindPartsInRegion3(region, RePart, math.huge)
    for i, RePart in pairs(partsInRegion) do
        if RePart.Parent:FindFirstChild("Humanoid") then                -- this part didnt work
             char = RePart.Player.Name
            DrowningStats.Value = DrowningStats.Value + 1
        else
            DrowningStats.Value = 0
        end
    end
end
while true do
wait(1)
    if DrowningStats.Value == 15 then
        char.Humanoid:TakeDamage(15)   -- This part didn't work.
    else
        print("User still has air")
    end
end

I see your issue, you are creating two while true loops. Lua reads code from top to bottom just like people read books.
So in your case it is forever stuck in the first while loop and will NEVER reach the second while loop since it is infinitely stuck on the first loop.

You could add that other while loop on a coroutine and it would start to execute the other loop

1 Like