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

Need to make a buy sign but can not get while true do to stop?

Asked by 5 years ago

hi i am making a game where you would buy proprieties and own them for a duration and while you own it you will be paid the amount in the code. My problem is i have put the "while true do" loop in the code and cant seem to get the code to go past it and it just keeps giving money. how would i fix this? Thank you all for your help.

local Duration = script.Parent.Duration.Value
local Owner = script.Parent.Owner
local pole = script.Parent.Pole
local Banner = script.Parent.Banner
Owned = false



function onClicked(player)
local money = player.leaderstats.Money
    if money.Value >= 100 then
        money.Value = money.Value - 100
        Owned = true
        Owner.Value = player.Name
        pole.Transparency = 1
        Banner.Transparency = 1
    while Owned == true do
        money.Value = money.Value + 1
        wait(1)
    end 
    wait(Duration)
        Owned = false
        Owner = "None"
        pole.Transparency = 0
        Banner.Transparency = 0
        Owner = "None"
    end
end 

script.Parent.ClickDetector.MouseClick:connect(onClicked)

2 answers

Log in to vote
0
Answered by
karlo_tr10 1233 Moderation Voter
5 years ago
Edited 5 years ago

You have to separate while loop because code after that won't run. Also, you are allowing a player to buy plot multiple times so you have to make check for that too. This code is not tested and might not work but I decided to take a few mins and fix it.

local Duration = script.Parent.Duration.Value
local Owner = script.Parent.Owner
local pole = script.Parent.Pole
local Banner = script.Parent.Banner
Owned = false

script.Parent.ClickDetector.MouseClick:Connect(function(player)
    local money = player.leaderstats.Money
    if Owned == true then return end
    if money.Value >= 100 then
        money.Value = money.Value - 100
        Owned = true
        Owner.Value = player.Name
        pole.Transparency = 1
        Banner.Transparency = 1
        wait(Duration)
        Owned = false
        Owner = "None"
        pole.Transparency = 0
        Banner.Transparency = 0
        Owner = "None"
    end
end)

spawn(function()
    while true do
        if Owned then
            local money = game.Players:FindFirstChild(Owner.Value).leaderstats.Money
            if money then
                money.Value = money.Value + 1
                wait(1)
            end
        else break
        end
    end
end)






Ad
Log in to vote
0
Answered by 5 years ago

Use coroutines, You are doing a while loop wich means it will yield the current thread until the loop is ended, use coroutine.create and coroutine.resume to make a new thread, here the code goes

local Duration = script.Parent.Duration.Value
local Owner = script.Parent.Owner
local pole = script.Parent.Pole
local Banner = script.Parent.Banner
Owned = false



function onClicked(player)
local money = player.leaderstats.Money
    if money.Value >= 100 then
        money.Value = money.Value - 100
        Owned = true
        Owner.Value = player.Name
        pole.Transparency = 1
        Banner.Transparency = 1
coroutine.resume(coroutine.create(function() -- run the while in a new thread to prevent yielding

    while Owned == true do
        money.Value = money.Value + 1
        wait(1)
    end 
end))
    wait(Duration)
        Owned = false
        Owner = "None"
        pole.Transparency = 0
        Banner.Transparency = 0
        Owner = "None"
    end
end 

script.Parent.ClickDetector.MouseClick:connect(onClicked)

BONUS: I'll give you a little example of coroutines, and how to use them

local MyCoroutine = coroutine.create(function()
-- your function, this is an example
while wait(1) do
print("Im working!")
end
end)

-- MyCoroutine is suspended, it means that you have to resume it to make it load

coroutine.resume(MyCoroutine) -- Load the coroutine
local SecondCoroutine = coroutine.create(function()
while wait(1) do
print("Im working too!")
end
end)
coroutine.resume(SecondCoroutine)
-- As you may see the two loops run at the same time!

The way to use coroutines is to load code at once without yielding the entire code, wich makes the code slower, or even not load, So when you use coroutines everything loads in other threads and all load at the same time!

0
If this helped, make sure to mark this question as answered! maumaumaumaumaumua 628 — 5y

Answer this question