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)
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
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
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
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.