Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make this water which is a brick slowly kill the player standing in it?

Basically the title.

local touching = false
local player = game:GetService("Players").LocalPlayer

local function onTouch(part)
    local hum = player:FindFirstChild("Humanoid")

    if hum then
        touching = true

        repeat
            hum:TakeDamage(5)
            wait(1)
        until not touching 
    end
end

local function onTouchEnd(part)
    local hum = part.Parent:FindFirstChild("Humanoid")

    if hum then 
        touching = false
    end
end

script.Parent.Touched:Connect(onTouch)
script.Parent.TouchEnded:Connect(onTouchEnd)

2 comments

0

I don't think it will run onTouchEnd() because the repeat is running. This may not be the case though.

0

I have edited my answer and fixed your problem. Let me know if there are any other problems. Also, if I have solved your problem, please make sure to accept my answer.

2 answers

✓ Accepted answer
local debounce = false
local i = 0

function onTouch(char)
    if char.Parent:FindFirstChild("Humanoid") and debounce == false then 
		debounce = true
		i = i + 1
        local hum = char.Parent.Humanoid
		if i == 1 then
			char.Parent.Health.Disabled = true
        	repeat
           		hum.Health = hum.Health-5
            	wait(1)
        	until debounce == false
			i = 0
			char.Parent.Health.Disabled = false
		end
    end
end

function touchedLand()
    debounce = false
end

script.Parent.Touched:Connect(onTouch)
workspace.Baseplate.Touched:Connect(touchedLand) --Edit this line 'workspace.Baseplate' to the part that is considered land.

This script would be placed under the part.

3 comments

0

I fixed it, it was the mesh was breaking it. But now it rapidly kills you when you walk in it. Any fix?

1

Try now. It turns out that the debounce is needed, my apologies.

0

than you so much :)

Put this in a ServerScript in SSS.

touching = false
script.Parent.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit)
local Hum = game.Workspace:WaitForChild(player.Name).Humanoid
if hit.Parent:FindFirstChild(Hum) ~= nil then
	touching = true
	while touching do
			Hum.Health = Hum.Health - 10
		end
	end
end)
script.Parent.TouchEnded:Connect(function(hit)
touching = false
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.