Why is this not Possible?

I’m making a script that runs a print constantly if the part detects a humanoid but then stops if the humanoid steps off.

script.Parent.Touched:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		repeat
			wait(1)
			print(hit.Parent.Name)
		until hit.TouchedEnded
	end
end)
1 Like

Do you get any error messages?

I’m not sure how to explain, but I think ‘TouchEnded’ can only be used to start the function. Consider using a boolvalue which enables when the script detects a humanoid, and make the print repeat until the boolvalue is set to ‘false’.

This wouldn’t work as you think it would. You can’t do that. I’m gonna take a wild guess, assuming that an RBXScriptSignal (an event) is truthy, that repeat will run only once.

You can simply do something else.


script.Parent.Touched:connect(function(hit)
    local previous = #script.Parent:GetTouchingParts()-1
	if hit.Parent:FindFirstChild("Humanoid") then
		repeat
			wait(1)
			print(hit.Parent.Name)
		until #script.Parent:GetTouchingParts() == previous
	end
end)
--i think this would work

Update: Nevermind this wouldn’t work because if another part was touching and stopped touching the loop would stop even though the player is still touching

2 Likes

yeah its says not member of part,

You can literally just do this.

part.TouchEnded:Wait()

This yields the script until TouchEnded fires.

Not like that. He wants something to loop over and over, using only

part.TouchEnded:Wait()

will just yield/ stop the script.

WOW how did you figure that out so fast?

Well, really you just have to know stuff that aren’t always used such as :GetTouchingParts() and think logically to figure out how to use them.

Although the code I sent is not perfect. It wouldn’t always function correctly.

1 Like

Can the TouchEnded be connected with a :Wait()? If so that would be nice, but not all events can have :Wait() only specific ones.

Actually they all do. So this works :+1:

3 Likes

I’ll try come up with a good script

Oh really? Didn’t know that, I’ve been informed multiple times that not all events can have :Wait() I guess that’s wrong.

@ffancyaxax12 Then it would be better to use what @THEsuperTERROR suggested using part.TouchEnded:Wait(), although still it wouldn’t always work, if another part stopped touching and that part wasn’t the player

1 Like

Thanks A bunch Guys :), I’m not new at scripting, but still enjoy learning

1 Like

Here is a way that would work 100% of the time

local isTouching = false
local currentChar 

script.Parent.Touched:connect(function(hit)
    isTouching = true      
    currentChar = hit.Parent

	if hit.Parent:FindFirstChild("Humanoid") then
        isTouching = true      
        currentChar = hit.Parent
		repeat
			wait(1)
			print(hit.Parent.Name)
		until not(isTouching)
	end
end)

script.Parent.TouchEnded:connect(function(hit)
      if hit.Parent == currentChar then
           isTouching = false
           currentChar = nil
      end
end)

Another problem would be if multiple characters touched the part. But from the looks of it you only want it to be printing if any character is touching, it doesn’t have to be a specific one

2 Likes
CharactersTouching = {}

Part = script.Parent

Part.Touched:connect(function(Hit)
	local Character = Hit.Parent
	
	-- Better make sure it's a player
	if not Character:FindFirstChildWhichIsA("Humanoid") then return end
	
	-- Don't want multiple loops to fire at once for a single player.
	if not table.find(CharactersTouching, Character) then
		
		-- Adding player's character to list,
		-- to prevent multiple loops firing at once for that user.
		table.insert(CharactersTouching, Character)
		
		local Touching = true
		
		-- Apparently i need to reserve a variable for the connection
		local TouchEndedConnection
		
		TouchEndedConnection = Part.TouchEnded:Connect(function(HitEnded)
			if HitEnded.Parent == Character then
				Touching = false
				TouchEndedConnection:Disconnect()
				
				table.remove(CharactersTouching, table.find(CharactersTouching, Character))
			end
		end)
		
		while Touching do
			
			wait(1)
			print("Test!")
			
		end
	end
end)

Note: Because the loop stops when a character stops touching the part in which case the player immediatly gets removed from the table, it can get spammed when the player rapidly touches and stops touching the part.

Edit: It does seem to work flawlessly for the rest, though. :slight_smile:

1 Like