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

My script prints multiple times when it's touched, and it should only once per touch. How to fix?

Here's an example:

Player = 0

while task.wait() do
    local Zone = workspace:GetPartsInPart(script.Parent)
    for i, v in pairs(Zone) do
		if v.Name == "UpperTorso" then
			wait()
				Player = 1
		end
	end
	if Player == 1 then
		print("player do exist")
		OpenAnim:Play()
		wait(0.71)
		OpenAnim:AdjustSpeed(0)
		Player = 2
	elseif Player == 2 then
		Player = 0
		OpenAnim:AdjustSpeed(1)
		print("Player dont exist")
	end
	end

Every time my character overlaps with the script parent, OpenAnim loops when it should just play once per interaction and freeze.

1 answer

The code is in a loop, so it repeatedly runs through the code many times per second. In this case, as long as Player is equal to one it will loop as many times as it can before Player == 2. I would add debounce argument to your if statement. Try what I have written below. I'm not sure if it will work in the context of your code but hopefully it gives you an idea. I hope this helps!

Player = 0
local debounce = true

while task.wait() do
	local Zone = workspace:GetPartsInPart(script.Parent)
	for i, v in pairs(Zone) do
		if v.Name == "UpperTorso" then
			wait()
			Player = 1
		end
	end
	if Player == 1 and debounce == true then
		print("player do exist")
		OpenAnim:Play()
		debounce = false
		wait(0.71)
		debounce = true
		OpenAnim:AdjustSpeed(0)
		Player = 2
	elseif Player == 2 and debounce == true then
		Player = 0
		OpenAnim:AdjustSpeed(1)
		print("Player dont exist")
	end
end

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