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

Why does this print several times?

local function onRenderStep(deltaTime)--DeltaTime is the time (in seconds) that has elapsed since the previous frame
	for _,v in pairs(doors:GetChildren())do
		local sensor = v.Sensor
		local distance = player:DistanceFromCharacter(sensor.Position)
		local doorClosed = v.Closed
		if distance < doorDetectionDistance and doorClosed.Value then--Opening door if in distance
			doorClosed.Value = false
			print("Opening door")
			moveDoors(v,1)
		elseif distance > doorDetectionDistance and doorClosed.Value == false then--closing door
			moveDoors(v,-1)
			doorClosed.Value = true
			print("Door Closed")
		end
	end
end
RunService.RenderStepped:Connect(onRenderStep)

"Opening door" prints once, but when I walk away "Door Closed" prints multiple times. Usually 37times

1 comment

0

please mark my answer as the correct answer, thanks!

2 answers

✓ Accepted answer

You can use debounce to prevents spam.

debounce = false

local function onRenderStep(deltaTime)--DeltaTime is the time (in seconds) that has elapsed since the previous frame
    for _,v in pairs(doors:GetChildren())do
        local sensor = v.Sensor
        local distance = player:DistanceFromCharacter(sensor.Position)
        local doorClosed = v.Closed
        if distance < doorDetectionDistance and doorClosed.Value then--Opening door if in distance
            doorClosed.Value = false
	    debounce = true
            print("Opening door")
	    debounce = false
            moveDoors(v,1)
        elseif distance > doorDetectionDistance and doorClosed.Value == false then--closing door
            moveDoors(v,-1)
            doorClosed.Value = true
	    debounce = true
            print("Door Closed")
	    debounce = false
        end
    end
end
RunService.RenderStepped:Connect(onRenderStep)

3 comments

0

Do you have other question, or need explains? Ask below comment!

0

oml you're right i do need a debounce to prevent the spam from renderstep. For some reason I thought the doorClosed.Value would handle it

0

If my answer is correct, mark this as the answer, thanks!

Shift the doorClosed.Value = true line above the moveDoors(v,-1) line, it's probably yielding and causing the value to be false for the small window it yields for.

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