Hello, I’m tryng to make a part that while is touched it prints or do something only 1 time and not repeat a lot of times like the touched function.
So basically when you use the touched function and you move over the part with that function that action happens a lot of times but I need it to happen only 1 time even if I’m moving over the part.
I tryed something like this but still the same thing.
.Touched functions are unstable and you may experience having the action fired more than once, which is very common in these situations. I’d recommend you adding debounces, and other securities (such as wait(3) variable = false) to refrain from these things from happening. RemoteEvents would also be useful in these situations if necessary.
You could use a boolean variable to ‘deactivate’ the part for a short time when it is touched.
local debouncer = true
local coolDownTime = 5
script.Parent.Touched:Connect(function()
if (debouncer) then
debouncer = false
print("Touched")
wait(5)
debouncer = true
end
end)
I didn’t understand the question at first. I thought you meant you wanted some kind of looping code that would run only if someone/thing was touching a part.
Use that same idea but instead you just do a repeat loop.
Do something like “repeat until touchFinished” and set touchFinished to true at the touch ended event
Example:
local t = 0
local touchHasEnded
part.Touched:Connect(function()
touchHasEnded = false
repeat
t += 1
wait(1)
until touchHasEnded
print(t)
end)
part.TouchEnded:Connect(function()
touchHasEnded = true
end)
So basically While P1 is touched the doors opens and keeps opened and after the player leaves the P1 the door closes but if repeats that many times the the door will open and open many times too.
I’m sorry it’s hard for me to explain sometimes, my native language is not English
Oh wait so you want it to close after the touch ends and NOT open anymore?
So you can use the connection method:
local connect1
local connect2
local touchEnd
connect1 = part.Touched:Connect(function()
connect1:Disconnect() -- Prevents the touched even happen again
repeat
wait(1)
until touchEnd
end)
connect2 = part.TouchEnded:Connect(function()
touchEnd = true
connect2:Disconnect() -- Also prevents the touch end of happening again
end)
If ‘P1’ is being used as a non-colliding/invisible hitbox then you can use my function to check if a player is inside a brick:
local rs = game:GetService("RunService")
local ps = game:GetService("Players")
local function isInsideBrick(position, brick)
local v3 = brick.CFrame:PointToObjectSpace(position)
return (math.abs(v3.X) <= brick.Size.X / 2)
and (math.abs(v3.Y) <= brick.Size.Y / 2)
and (math.abs(v3.Z) <= brick.Size.Z / 2)
end
local function getPlayersInsideZone(zone)
local list = {}
for _, player in ipairs(ps:GetPlayers()) do
if (player.Character) then
local torso = player.Character:FindFirstChild("HumanoidRootPart")
if (torso and isInsideBrick(torso.Position, zone)) then
table.insert(list, player)
end
end
end
return list
end
local open = false
rs.Heartbeat:Connect(function()
if (#getPlayersInsideZone(script.Parent) > 0) then -- someone is standing inside the region
if (not open) then
print("Open")
open = true
--code to open the door
end
else -- nobody is inside the region
if (open) then
print("Close")
open = false
--code to close the door
end
end
end)