Hello, I am currently working on a game in which I need the Roblox provided water to deal damage to the player on touch. I have done this by just adding the water and then making an invisible block that doesn’t collide with the player and placed it at the exact position the water is.
This is the script I am using:
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 0.5
end
end)
It’s very basic, I know, but so far it hasn’t lead to any isses. The problem is that today I figured out that it was possible to just not move in the water and it will stop dealing damage. This is the thing I am trying to prevent.
Any help is appreciated!
local DAMAGE = 10
local INTERVAL = 1/30
local touchingHumanoids = {}
script.Parent.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if not humanoid or humanoid.RootPart == hit then return end
if touchingHumanoids[humanoid] then
touchingHumanoids[humanoid] += 1
else
touchingHumanoids[humanoid] = 1
end
end)
script.Parent.TouchEnded:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if not humanoid or humanoid.RootPart == hit then return end
if touchingHumanoids[humanoid] then
touchingHumanoids[humanoid] -= 1
if touchingHumanoids[humanoid] == 0 then
touchingHumanoids[humanoid] = nil
end
end
end)
while true do
for humanoid in pairs(touchingHumanoids) do
-- if the humanoid is destroyed, remove it from the loop
if not humanoid:IsDescendantOf(game) then
touchingHumanoids[humanoid] = nil
continue
end
humanoid:TakeDamage(DAMAGE)
end
task.wait(INTERVAL)
end