Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

?Script Printing More than I need it to?

Asked by
KixWater 126
3 years ago

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
0
Sorry about the wrong answer, I left an edited code and an explanation at the bottom that should hopefully work cmgtotalyawesome 1418 — 3y
0
it fixes one thing which is the teleporting, but the problems are not completely fixed, sorry. KixWater 126 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

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.

0
it still wont work. and the value of "TotalCountedPlayers" goes up more thne it needs to, the same amount as it prints KixWater 126 — 3y
Ad

Answer this question