im making a drowning system using collectionservice, but when i playtest even tho all of the parts are tagged, only one works
i tried to put the code in loops but it still didnt work, and none of the posts on the dev forum helped
local CollectionService = game:GetService("CollectionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DAMAGE_PER_SECOND = 8
local TIME_BEFORE_DAMAGE = 3
for i, water in CollectionService:GetTagged("Water") do
local inWater = {}
water.Touched:Connect(function(object)
if (object.Name == "Head" and inWater[object.Parent] == nil) then
inWater[object.Parent] = TIME_BEFORE_DAMAGE
ReplicatedStorage.Events.InWater:FireClient(game.Players:GetPlayerFromCharacter(object.Parent),true)
end
end)
water.TouchEnded:Connect(function(object)
if (object.Name == "Head") then
ReplicatedStorage.Events.InWater:FireClient(game.Players:GetPlayerFromCharacter(object.Parent),false)
local n = inWater[object.Parent]
inWater[object.Parent] = nil
end
end)
while (true) do
for char, count in pairs(inWater) do
if (count > 0) then
inWater[char] -= 1
else
char.Humanoid:TakeDamage(DAMAGE_PER_SECOND)
end
end
task.wait(1)
end
end
testing all the parts with the tag water, only one works