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

How do you check if a part is still touching another part?

I am trying to make an electric wall that deals splash damage to everyone who touches it. the longer you touch it, the more damage it deals. How can I go to do this.

1 answer

✓ Accepted answer

Ok, so I just read an article on stuff like this and I think I got it.

So when the part touches the wall, you want a detection script, so the script knows there is a part. Then you want to check if the part that touched the wall is part of a character by checking for a humanoid. Lastly, if it was part of a character, you want to record the humanoid's parent (the character) in a list so you can have multiple people being affected.

local touching = {}
local wall = workspace.wall --Change this to the wall.

local function onTouch(part)
	local humanoid = part.Parent:FindFirstChild("Humanoid")

	if not humanoid then
		humanoid = part.Parent.Parent:FindFirstChild("Humanoid") --Used if the part of the character that touched the wall was not an arm or a leg, but an accessory.
	end

	if not humanoid then return end --If no humanoid was found then stop the function.
	touching.insert(humanoid.Parent)

	humanoid:takeDamage(10) --Take some initial starting damage.
end

wall.Touched:Connect(onTouch)
  • Quick note I'm just winging it from here.

I don't quite know how to do the damage, but I can give you this. Every couple tenths of a second or so, damage all players in the list "touching" by a few HP.

for i in pairs(touching) do
	touching[i]:FindFirstChild("Humanoid"):takeDamage(5)
end

That will damage all of the players in "touching", but I don't know how to loop it. You could maybe hook up a RemoteEvent, telling it to run that script every second or something.

Now you want to check if any parts have stopped touching the wall.

local function onTouchEnded(part)
	local humanoid = part.Parent:FindFirstChild("Humanoid")

	if not humanoid then
		humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
	end

	if not humanoid then return end

	for i in pairs(touching) do
		if humanoid.Parent.Name = touching[i].Name then
			touching[i]:Remove()
		end
	end
end

wall.TouchEnded:Connect(onTouchEnded)

That's all the help I can offer.

local touching = {}
local wall = workspace.wall --Change this to the wall.

local function onTouch(part)
	local humanoid = part.Parent:FindFirstChild("Humanoid")

	if not humanoid then
		humanoid = part.Parent.Parent:FindFirstChild("Humanoid") --Used if the part of the character that touched the wall was not an arm or a leg, but an accessory.
	end

	if not humanoid then return end --If no humanoid was found then stop the function.
	touching.insert(humanoid.Parent)

	humanoid:takeDamage(10) --Take some initial starting damage.
end

local function onTouchEnded(part)
	local humanoid = part.Parent:FindFirstChild("Humanoid")

	if not humanoid then
		humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
	end

	if not humanoid then return end

	for i in pairs(touching) do
		if humanoid.Parent.Name = touching[i].Name then
			touching[i]:Remove()
		end
	end
end

local function damageAllPlayers()
	for i in pairs(touching) do
		touching[i]:FindFirstChild("Humanoid"):takeDamage(5)
	end
end

wall.Touchet:Connect(onTouch)
wall.TouchEnded:Connect(onTouchEnded)

:)

2 comments

0

I forgot to change the :takeDamage value in the damageAllPlayers function to a variable, because you said it damages by more every time, but I bet you can do that anyway.

0

thank you so much, so sorry I did not accept it fast enough. Yea this worked I improved it a bit too thanks!

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