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

Is it possible to make some line of code fire WHILE a part is being touched?

I'm trying to make a spleef like game and in it i want an ice mechanic where you step on it and it would crack, and then it would eventually break. I use the .touched event to know when a player is touching the part. But that only fires when the player BEGINS to touch the part. I need it to fire while the player is touching and stop firing when the player has stopped. So the main question is that if its possible to make something fire while a player is touching a part? And if so, how?

3 answers

Use BasePart.TouchEnded event. It fires when the player untouches the part.

local isCracking = false

Part.Touched:Connect(function(otherPart) -- if the player touches the part
	isCracking = true
end)

Part.TouchEnded:Connect(function(otherPart) -- if the player untouches
	isCracking = false
	print("Player untouched the part. Part stopped cracking.")
end)

while isCracking do -- if isCracking is true, it will repeat printing every 1 second
	print("The part is cracking.")
end

2 comments

0

you could do GettouchingParts()

0

I have figured out the solution from the person that commented on your answer. Thank you for your help and time! I have figured it out

I figured out the solution. Instead of making the code fire while its touching. I made a part that will be invisible directly on top of the ice so that when the person is on the ice, they will be inside the part above the ice.

local ice = script.Parent
local partdetector = ice.Part


while wait(0.5) do
	for i,v in pairs(game.Workspace:GetPartsInPart(partdetector)) do
		if v.Parent:FindFirstChild("Humanoid") then
			print("theres a person on it!")
		end
	end
end

This works in detecting if a person is on the ice, also being inside the detecting part. Thank you for your help but I have found the solution thanks to puppynniko's comment recommendation.

Try using in your code within each of the iceblocks:

local ice = script.Parent
local iceDetect = ice.Detector
local iceHP = 3 -- make the ice block break in 6 seconds

while true do
	if touchDetect.touched:Connect(function()(
		iceHP = iceHP - 1 -- take away one health from ice

		print("Ice has taken damage!") -- do what you want to do
		
		if iceHP == 0 then
			ice:Remove() -- gets rid of ice
		end

		wait(2) -- as if it's breaking to your weight every two seconds
	end)
end

this is an inefficient way but it works.

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