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

?Script Printing More than I need it to?

I'm making a round script where when somebody steps on a part, they get teleported back to the lobby and the script adds one value for TotalCountedPlayers that stepped on that part. The problem is that I set a print function and it's printing more than once even with debounce.

if plr and char and plr:FindFirstChild('GameTag') then
				for _, TaggedParts in pairs(CollectionService:GetTagged("ObbyRewardsTag")) do
					TaggedParts.Touched:Connect(function(hit)
						local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
						local char = plr.Character
						local hum = char:FindFirstChild("Humanoid")
						local humRootPart = char:FindFirstChild("HumanoidRootPart")
						local DB = false

						if plr and char and DB == false and hum and humRootPart and plr:FindFirstChild("GameTag") then
							DB = true
							TotalCountedPlayers.Value = TotalCountedPlayers.Value + 1
							plr.LeaderStatsFolder.Stonks.Value = plr.LeaderStatsFolder.Stonks.Value + RandomAmount
							humRootPart.CFrame = LobbySpawner[1].CFrame
							table.remove(LobbySpawner, 1)
							DB = false
							wait(.5)
							print("Went Up By One")
						end
					end)
				end
			end
		end

2 comments

0

Sorry about the wrong answer, I left an edited code and an explanation at the bottom that should hopefully work

0

it fixes one thing which is the teleporting, but the problems are not completely fixed, sorry.

1 answer

Your problem is simply that you are yielding after you set debounce to false. Debounce will only work if you set it back to false after you wait the desired amount of time. This is a super simple fix and would cause your script to look like this:

local DB = false

if plr and char and plr:FindFirstChild('GameTag') then
                for _, TaggedParts in pairs(CollectionService:GetTagged("ObbyRewardsTag")) do
                    TaggedParts.Touched:Connect(function(hit)
                        local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
                        local char = plr.Character
                        local hum = char:FindFirstChild("Humanoid")
                        local humRootPart = char:FindFirstChild("HumanoidRootPart")
                        
                        if plr and char and DB == false and hum and humRootPart and plr:FindFirstChild("GameTag") then
                            DB = true
                            TotalCountedPlayers.Value = TotalCountedPlayers.Value + 1
                            plr.LeaderStatsFolder.Stonks.Value = plr.LeaderStatsFolder.Stonks.Value + RandomAmount
                            humRootPart.CFrame = LobbySpawner[1].CFrame
                            table.remove(LobbySpawner, 1)
                            wait(.5)
                            print("Went Up By One")
                            DB = false
                        end
                    end)
                end
            end
        end

Simple as that! Let me know if this still gives you problems and I'll see if I can find anything else.

EDIT: What I've changed is where you are setting the debounce to false. You used to be setting it to false every single time the event ran (at least I'm assuming you're using a touched event) which means that the debounce will never be true. If you set it at the beginning of the script instead of inside the event your problem should now be fixed.

1 comment

0

it still wont work. and the value of "TotalCountedPlayers" goes up more thne it needs to, the same amount as it prints

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