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 a player slowly get damaged while touching a brick?

My goal is to make a drowning script. So while the head of the player is touching the water it should take away 10 from the humanoid health.

I've attempted this and came up with this but it is not working. This script is in StarterCharacterScripts.

--Variables
local water1 = workspace.water1
local water2 = workspace.water2
local humanoid = script.Parent.Humanoid
local name = script.Parent.Name
local isDrowing = false
local drowningDamage = 10

--Functions
local function onTouch(part)
	if part.Parent.Name == name then
		if part.Name == "Head" then
			isDrowing = true
		end
	end
end
 
local function onTouchEnded(part)
	if part.Parent.Name == name then
		if part.Name == "Head" then
			isDrowing = false
		end
	end
end

--While Under Water
while isDrowing == true do
	humanoid.Health = humanoid.Health + (drowningDamage * -1) print("Drowing")
	wait(1)
end

--Connect Functions
water1.Touched:Connect(onTouch)
water1.TouchEnded:Connect(onTouchEnded)
water2.Touched:Connect(onTouch)
water2.TouchEnded:Connect(onTouchEnded)

2 answers

✓ Accepted answer

Hi there, I'm creating another answer for you as the previous answer should not be used due to filtering enabled, and inconsistent damage-giving.

I'm presuming you put all parts that give you damage in a folder in a workspace (can of course be changed)

Note that I have not implemented any solutions if certain objects do not exist.

local Players = game:GetService("Players");
local DamageParts = (workspace:WaitForChild("DamageParts")):GetChildren();
local Damage = 5;
local debounce = {}; 

for num = 1,#DamageParts do
	local Part = DamageParts[num];

	Part.Touched:Connect(function(hit)
		if hit and hit.Parent:FindFirstChildOfClass("Humanoid") then
			local Player = Players:GetPlayerFromCharacter(hit.Parent);
			if Player then
				if debounce[Player.UserId] then return end
				debounce[Player.UserId] = true

				while (debounce[Player.UserId]) do
					local Character = Player.Character;
					if Character then Character:FindFirstChildOfClass("Humanoid"):TakeDamage(Damage) end
				end
			end
		end
	end
	
	Part.TouchEnded:Connect(function(hit)
		if hit and hit.Parent:FindFirstChildOfClass("Humanoid") then
			local Player = PLayers:GetPlayerFromCharacter(hit.Parent)
			if Player then
				debounce[Player.UserId] = nil
			end
		end
	end)
end

This is a simple script that should be placed in ServerScriptService. Note that you should do some precautions and optimization. You also have some other options to find out if a player is touching a part and then damaging. This could be (API)Part:GetTouchingParts() and Region3.

You can get back to that later once you've learned a bit more.

Hello, CIuee!

I have written a script for you while explaining what each line does :]

-- Place this as a LOCALSCRIPT inside a part :]
part = script.Parent -- This line is a variable used to detect the thing that the SCRIPT is inside.

damage = 5 -- This is a number variable, change it to the damage you like!

part.Touched:Connect(function(hit) -- This function automatically detects if the part is touched note : hit could be anything, like... balloon.

if hit.Parent and hit.Parent:FindFirstChild("Humanoid") then -- Used to detect if the thing has a humanoid.
    hit.Parent.Humanoid:TakeDamage(damage) -- The thing's humanoid will take the "variable" damage.
wait(1) -- Wait before running the function again.
   end -- End (if)
end) -- End (Function)

-- Have fun :]!

-- Hope i helped.

---- AIphanium

3 comments

0

About the debounce part you may wanna do that localy because if 2 people step on the brick and someone gets damaged the other guy won't for 1 second because of the debounce. You may want to do the debounce localy with a remote event.

0

Hmm, i think changing it from a SCRIPT to a LOCALSCRIPT may help :/

0

This would not work as Local Script. From Workspace you can run only "Normal" scripts.

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