I’m making a game that gives a player leaderstats every minute that’s passed, it works, its just I have a gamepass that boosts these speeds. But it requires a button to purchase, and I made a button wheather you can toggle the gamepass on/off using loops. It’s just when i turn it off/on, the loops don’t seem to stop. I need assistance on how I could fix or improve this. Here is the code in a localScript inside my gui.
local marketplaceservice = game:GetService("MarketplaceService")
local player = game.Players.LocalPlayer
local plrHidden = player:WaitForChild("hiddenstats")
local plrStats = player:WaitForChild("leaderstats")
local boosPassId = 668758510 --replace with your gamepass id
local boostButton = script.Parent:WaitForChild("CanvasGroup"):WaitForChild("Buttons"):WaitForChild("ToggleBoost"):WaitForChild("ImageButton") -- frame
local boostButton2 = script.Parent:WaitForChild("CanvasGroup"):WaitForChild("Buttons"):WaitForChild("ToggleBoost"):WaitForChild("ImageButton2") -- frame
local timeText = script.Parent.CanvasGroup.LeaderstatsFrame.TimeFrame:WaitForChild("TimeText")
local Cash = plrStats:WaitForChild("Cash")
local seconds = plrHidden:WaitForChild("Seconds")
local minutes = plrHidden:WaitForChild("Minutes")
local RS = game:GetService("ReplicatedStorage")
local rent = RS.RemoteEvents:WaitForChild("RentAlert")
local rentGiver = RS.RemoteEvents:WaitForChild("RentGiver")
rentGiver.OnClientEvent:Connect(function(money)
Cash.Value += money
print("given "..money.." to "..player.Name)
end)
boostButton.MouseButton1Click:Connect(function()
if marketplaceservice:UserOwnsGamePassAsync(player.UserId,boosPassId) then
print("player own gamepass")
if boostButton.Visible == true then
boostButton2.Visible = true
boostButton.Visible = false
while true do
wait(0.2)
timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
seconds.Value += 1
--print("Second + 1")
if seconds.Value == 60 then
--print("Rent")
rent:FireServer(player)
seconds.Value = 0
minutes.Value += 1
end
if seconds.Value <= 9 and minutes.Value <=9 then
timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
--print("s & m less than 9")
elseif seconds.Value >= 9 and minutes.Value >=9 then
timeText.Text = minutes.Value..":"..seconds.Value
--print("s & m more than 9")
elseif seconds.Value >= 9 and minutes.Value <=9 then
timeText.Text = "0"..minutes.Value..":"..seconds.Value
--print("s more than 9 m less than 9")
elseif seconds.Value <= 9 and minutes.Value >=9 then
timeText.Text = minutes.Value..":".."0"..seconds.Value
--print("m more than 9 s less than 9")
end
if minutes.Value == 24 then
minutes.Value = 0
seconds.Value = 0
--print("ResetTime")
end
end
else
marketplaceservice:PromptGamePassPurchase(player,boosPassId)
print("player doesnt own gamepass")
end
end
end)
boostButton2.MouseButton1Click:Connect(function()
if boostButton2.Visible == true then
boostButton.Visible = true
boostButton2.Visible = false
while true do
wait(0.5)
timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
seconds.Value += 1
--print("Second + 1")
if seconds.Value == 60 then
--print("Rent")
rent:FireServer(player)
seconds.Value = 0
minutes.Value += 1
end
if seconds.Value <= 9 and minutes.Value <=9 then
timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
--print("s & m less than 9")
elseif seconds.Value >= 9 and minutes.Value >=9 then
timeText.Text = minutes.Value..":"..seconds.Value
--print("s & m more than 9")
elseif seconds.Value >= 9 and minutes.Value <=9 then
timeText.Text = "0"..minutes.Value..":"..seconds.Value
--print("s more than 9 m less than 9")
elseif seconds.Value <= 9 and minutes.Value >=9 then
timeText.Text = minutes.Value..":".."0"..seconds.Value
--print("m more than 9 s less than 9")
end
if minutes.Value == 24 then
minutes.Value = 0
seconds.Value = 0
--print("ResetTime")
end
end
end
end)
if boostButton.Visible == true then
while true do
wait(0.5)
timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
seconds.Value += 1
--print("Second + 1")
if seconds.Value == 60 then
--print("Rent")
rent:FireServer(player)
seconds.Value = 0
minutes.Value += 1
end
if seconds.Value <= 9 and minutes.Value <=9 then
timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
--print("s & m less than 9")
elseif seconds.Value >= 9 and minutes.Value >=9 then
timeText.Text = minutes.Value..":"..seconds.Value
--print("s & m more than 9")
elseif seconds.Value >= 9 and minutes.Value <=9 then
timeText.Text = "0"..minutes.Value..":"..seconds.Value
--print("s more than 9 m less than 9")
elseif seconds.Value <= 9 and minutes.Value >=9 then
timeText.Text = minutes.Value..":".."0"..seconds.Value
--print("m more than 9 s less than 9")
end
if minutes.Value == 24 then
minutes.Value = 0
seconds.Value = 0
--print("ResetTime")
end
end
end
BoostButton2 is the on button, its appears once BoostButton1 is clicked and can only turn on if you have the gamepass, it changes the speed to 0.2 rather than 0.5 as can be seen, its just the loops stack, and don’t turn off once i click the button, which I need assistance with.
so you want the non-booster loop to stop once the booster is on? and also add a way to stop stacking loops? try adding a one time only bool. So if the booster is turned on it will check if the one time only variable is false to run. And when it runs the booster, make it set the one time bool to true
Yes, i want the non booster loop to stop once i turn the booster loop on and vice versa and stop stacking them. How could i implement this one time bool
local marketplaceservice = game:GetService("MarketplaceService")
local player = game.Players.LocalPlayer
local plrHidden = player:WaitForChild("hiddenstats")
local plrStats = player:WaitForChild("leaderstats")
local boosPassId = 668758510 --replace with your gamepass id
local boostButton = script.Parent:WaitForChild("CanvasGroup"):WaitForChild("Buttons"):WaitForChild("ToggleBoost"):WaitForChild("ImageButton") -- frame
local boostButton2 = script.Parent:WaitForChild("CanvasGroup"):WaitForChild("Buttons"):WaitForChild("ToggleBoost"):WaitForChild("ImageButton2") -- frame
local timeText = script.Parent.CanvasGroup.LeaderstatsFrame.TimeFrame:WaitForChild("TimeText")
local Cash = plrStats:WaitForChild("Cash")
local seconds = plrHidden:WaitForChild("Seconds")
local minutes = plrHidden:WaitForChild("Minutes")
local RS = game:GetService("ReplicatedStorage")
local rent = RS.RemoteEvents:WaitForChild("RentAlert")
local rentGiver = RS.RemoteEvents:WaitForChild("RentGiver")
rentGiver.OnClientEvent:Connect(function(money)
Cash.Value = Cash.Value+ money
print("given "..money.." to "..player.Name)
end)
local OneTimeOnlyBool = false
local OneTimeOnlyBool2 = false
local BoostingEffect = false
boostButton.MouseButton1Click:Connect(function()
if OneTimeOnlyBool2 == false and marketplaceservice:UserOwnsGamePassAsync(player.UserId,boosPassId) then
OneTimeOnlyBool2 = true
BoostingEffect = true
print("player own gamepass")
if boostButton.Visible == true then
boostButton2.Visible = true
boostButton.Visible = false
while BoostingEffect == true do
wait(0.2)
timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
seconds.Value = seconds.Value+1
--print("Second + 1")
if seconds.Value == 60 then
--print("Rent")
rent:FireServer(player)
seconds.Value = 0
minutes.Value = minutes.Value+1
end
if seconds.Value <= 9 and minutes.Value <=9 then
timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
--print("s & m less than 9")
elseif seconds.Value >= 9 and minutes.Value >=9 then
timeText.Text = minutes.Value..":"..seconds.Value
--print("s & m more than 9")
elseif seconds.Value >= 9 and minutes.Value <=9 then
timeText.Text = "0"..minutes.Value..":"..seconds.Value
--print("s more than 9 m less than 9")
elseif seconds.Value <= 9 and minutes.Value >=9 then
timeText.Text = minutes.Value..":".."0"..seconds.Value
--print("m more than 9 s less than 9")
end
if minutes.Value == 24 then
minutes.Value = 0
seconds.Value = 0
--print("ResetTime")
end
end
else
marketplaceservice:PromptGamePassPurchase(player,boosPassId)
print("player doesnt own gamepass")
end
end
end)
boostButton2.MouseButton1Click:Connect(function()
if OneTimeOnlyBool == false and boostButton2.Visible == true then
OneTimeOnlyBool = true
boostButton.Visible = true
boostButton2.Visible = false
while BoostingEffect == false do
wait(0.5)
timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
seconds.Value = seconds.Value+1
--print("Second + 1")
if seconds.Value == 60 then
--print("Rent")
rent:FireServer(player)
seconds.Value = 0
minutes.Value = minutes.Value+1
end
if seconds.Value <= 9 and minutes.Value <=9 then
timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
--print("s & m less than 9")
elseif seconds.Value >= 9 and minutes.Value >=9 then
timeText.Text = minutes.Value..":"..seconds.Value
--print("s & m more than 9")
elseif seconds.Value >= 9 and minutes.Value <=9 then
timeText.Text = "0"..minutes.Value..":"..seconds.Value
--print("s more than 9 m less than 9")
elseif seconds.Value <= 9 and minutes.Value >=9 then
timeText.Text = minutes.Value..":".."0"..seconds.Value
--print("m more than 9 s less than 9")
end
if minutes.Value == 24 then
minutes.Value = 0
seconds.Value = 0
--print("ResetTime")
end
end
end
end)
if boostButton.Visible == true then
while BoostingEffect == false do
OneTimeOnlyBool = true
wait(0.5)
timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
seconds.Value = seconds.Value+1
--print("Second + 1")
if seconds.Value == 60 then
--print("Rent")
rent:FireServer(player)
seconds.Value = 0
minutes.Value = minutes.Value+1
end
if seconds.Value <= 9 and minutes.Value <=9 then
timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
--print("s & m less than 9")
elseif seconds.Value >= 9 and minutes.Value >=9 then
timeText.Text = minutes.Value..":"..seconds.Value
--print("s & m more than 9")
elseif seconds.Value >= 9 and minutes.Value <=9 then
timeText.Text = "0"..minutes.Value..":"..seconds.Value
--print("s more than 9 m less than 9")
elseif seconds.Value <= 9 and minutes.Value >=9 then
timeText.Text = minutes.Value..":".."0"..seconds.Value
--print("m more than 9 s less than 9")
end
if minutes.Value == 24 then
minutes.Value = 0
seconds.Value = 0
--print("ResetTime")
end
end
end
Hmm maybe try this one… (Try learning bool values too or workarounds)
local marketplaceservice = game:GetService("MarketplaceService")
local player = game.Players.LocalPlayer
local plrHidden = player:WaitForChild("hiddenstats")
local plrStats = player:WaitForChild("leaderstats")
local boosPassId = 668758510 --replace with your gamepass id
local boostButton = script.Parent:WaitForChild("CanvasGroup"):WaitForChild("Buttons"):WaitForChild("ToggleBoost"):WaitForChild("ImageButton") -- frame
local boostButton2 = script.Parent:WaitForChild("CanvasGroup"):WaitForChild("Buttons"):WaitForChild("ToggleBoost"):WaitForChild("ImageButton2") -- frame
local timeText = script.Parent.CanvasGroup.LeaderstatsFrame.TimeFrame:WaitForChild("TimeText")
local Cash = plrStats:WaitForChild("Cash")
local seconds = plrHidden:WaitForChild("Seconds")
local minutes = plrHidden:WaitForChild("Minutes")
local RS = game:GetService("ReplicatedStorage")
local rent = RS.RemoteEvents:WaitForChild("RentAlert")
local rentGiver = RS.RemoteEvents:WaitForChild("RentGiver")
rentGiver.OnClientEvent:Connect(function(money)
Cash.Value = Cash.Value+ money
print("given "..money.." to "..player.Name)
end)
local OneTimeOnlyBool = false
local OneTimeOnlyBool2 = false
local BoostingEffect = false
boostButton.MouseButton1Click:Connect(function()
if OneTimeOnlyBool2 == true then
print("booster off, returning to normal")
OneTimeOnlyBool2 = false
BoostingEffect = false
coroutine.resume(coroutine.create(function()
while BoostingEffect == false do
wait(0.5)
timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
seconds.Value = seconds.Value+1
--print("Second + 1")
if seconds.Value == 60 then
--print("Rent")
rent:FireServer(player)
seconds.Value = 0
minutes.Value = minutes.Value+1
end
if seconds.Value <= 9 and minutes.Value <=9 then
timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
--print("s & m less than 9")
elseif seconds.Value >= 9 and minutes.Value >=9 then
timeText.Text = minutes.Value..":"..seconds.Value
--print("s & m more than 9")
elseif seconds.Value >= 9 and minutes.Value <=9 then
timeText.Text = "0"..minutes.Value..":"..seconds.Value
--print("s more than 9 m less than 9")
elseif seconds.Value <= 9 and minutes.Value >=9 then
timeText.Text = minutes.Value..":".."0"..seconds.Value
--print("m more than 9 s less than 9")
end
if minutes.Value == 24 then
minutes.Value = 0
seconds.Value = 0
--print("ResetTime")
end
end
end))
return
end
if OneTimeOnlyBool2 == false and marketplaceservice:UserOwnsGamePassAsync(player.UserId,boosPassId) then
OneTimeOnlyBool2 = true
BoostingEffect = true
print("booster on")
print("player own gamepass")
if boostButton.Visible == true then
boostButton2.Visible = true
boostButton.Visible = false
while BoostingEffect == true do
wait(0.2)
timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
seconds.Value = seconds.Value+1
--print("Second + 1")
if seconds.Value == 60 then
--print("Rent")
rent:FireServer(player)
seconds.Value = 0
minutes.Value = minutes.Value+1
end
if seconds.Value <= 9 and minutes.Value <=9 then
timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
--print("s & m less than 9")
elseif seconds.Value >= 9 and minutes.Value >=9 then
timeText.Text = minutes.Value..":"..seconds.Value
--print("s & m more than 9")
elseif seconds.Value >= 9 and minutes.Value <=9 then
timeText.Text = "0"..minutes.Value..":"..seconds.Value
--print("s more than 9 m less than 9")
elseif seconds.Value <= 9 and minutes.Value >=9 then
timeText.Text = minutes.Value..":".."0"..seconds.Value
--print("m more than 9 s less than 9")
end
if minutes.Value == 24 then
minutes.Value = 0
seconds.Value = 0
--print("ResetTime")
end
end
else
marketplaceservice:PromptGamePassPurchase(player,boosPassId)
print("player doesnt own gamepass")
end
end
end)
boostButton2.MouseButton1Click:Connect(function()
if OneTimeOnlyBool == false and boostButton2.Visible == true then
OneTimeOnlyBool = true
boostButton.Visible = true
boostButton2.Visible = false
while BoostingEffect == false do
wait(0.5)
timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
seconds.Value = seconds.Value+1
--print("Second + 1")
if seconds.Value == 60 then
--print("Rent")
rent:FireServer(player)
seconds.Value = 0
minutes.Value = minutes.Value+1
end
if seconds.Value <= 9 and minutes.Value <=9 then
timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
--print("s & m less than 9")
elseif seconds.Value >= 9 and minutes.Value >=9 then
timeText.Text = minutes.Value..":"..seconds.Value
--print("s & m more than 9")
elseif seconds.Value >= 9 and minutes.Value <=9 then
timeText.Text = "0"..minutes.Value..":"..seconds.Value
--print("s more than 9 m less than 9")
elseif seconds.Value <= 9 and minutes.Value >=9 then
timeText.Text = minutes.Value..":".."0"..seconds.Value
--print("m more than 9 s less than 9")
end
if minutes.Value == 24 then
minutes.Value = 0
seconds.Value = 0
--print("ResetTime")
end
end
end
end)
if boostButton.Visible == true then
while BoostingEffect == false do
OneTimeOnlyBool = true
wait(0.5)
timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
seconds.Value = seconds.Value+1
--print("Second + 1")
if seconds.Value == 60 then
--print("Rent")
rent:FireServer(player)
seconds.Value = 0
minutes.Value = minutes.Value+1
end
if seconds.Value <= 9 and minutes.Value <=9 then
timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
--print("s & m less than 9")
elseif seconds.Value >= 9 and minutes.Value >=9 then
timeText.Text = minutes.Value..":"..seconds.Value
--print("s & m more than 9")
elseif seconds.Value >= 9 and minutes.Value <=9 then
timeText.Text = "0"..minutes.Value..":"..seconds.Value
--print("s more than 9 m less than 9")
elseif seconds.Value <= 9 and minutes.Value >=9 then
timeText.Text = minutes.Value..":".."0"..seconds.Value
--print("m more than 9 s less than 9")
end
if minutes.Value == 24 then
minutes.Value = 0
seconds.Value = 0
--print("ResetTime")
end
end
end
Make sure to set BoostingEffect To false Cause I only see one line of BoostingEffect And that is to turn on. But I don’t see where you are setting it to false
Oh, this is because roblox Lua focuses on a single loop and if the loop will continue to run, it wont run codes beneath it. try using coroutine for loops
if boostButton.Visible == true then
while BoostingEffect == false do
OneTimeOnlyBool = true
coroutine.resume(coroutine.create(function()
wait(0.5)
timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
seconds.Value = seconds.Value+1
--print("Second + 1")
if seconds.Value == 60 then
--print("Rent")
rent:FireServer(player)
seconds.Value = 0
minutes.Value = minutes.Value+1
end
if seconds.Value <= 9 and minutes.Value <=9 then
timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
--print("s & m less than 9")
elseif seconds.Value >= 9 and minutes.Value >=9 then
timeText.Text = minutes.Value..":"..seconds.Value
--print("s & m more than 9")
elseif seconds.Value >= 9 and minutes.Value <=9 then
timeText.Text = "0"..minutes.Value..":"..seconds.Value
--print("s more than 9 m less than 9")
elseif seconds.Value <= 9 and minutes.Value >=9 then
timeText.Text = minutes.Value..":".."0"..seconds.Value
--print("m more than 9 s less than 9")
end
if minutes.Value == 24 then
minutes.Value = 0
seconds.Value = 0
--print("ResetTime")
end
end))
end
end
yeah just like that. Try to implement them to your loops so it doesnt pause the script.
Edit: oh wait theres something wrong
Edit 2: you misplaced the coroutine… here:
if boostButton.Visible == true then
coroutine.resume(coroutine.create(function()
while BoostingEffect == false do
OneTimeOnlyBool = true
wait(0.5)
timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
seconds.Value = seconds.Value+1
--print("Second + 1")
if seconds.Value == 60 then
--print("Rent")
rent:FireServer(player)
seconds.Value = 0
minutes.Value = minutes.Value+1
end
if seconds.Value <= 9 and minutes.Value <=9 then
timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
--print("s & m less than 9")
elseif seconds.Value >= 9 and minutes.Value >=9 then
timeText.Text = minutes.Value..":"..seconds.Value
--print("s & m more than 9")
elseif seconds.Value >= 9 and minutes.Value <=9 then
timeText.Text = "0"..minutes.Value..":"..seconds.Value
--print("s more than 9 m less than 9")
elseif seconds.Value <= 9 and minutes.Value >=9 then
timeText.Text = minutes.Value..":".."0"..seconds.Value
--print("m more than 9 s less than 9")
end
if minutes.Value == 24 then
minutes.Value = 0
seconds.Value = 0
--print("ResetTime")
end
end
end))
end