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

Why isn't the if/else part of my program executing?

Asked by 6 years ago

So I've built an upgrader for my tycoon money blocks, and it executes the "Upgraded" function, but then when it gets to the "Add Money" function, it doesn't do anything. I feel like I might have phrased the code wrong but I'm not sure.

local block = script.Parent
local Tycoon = game.Workspace.Tycoon
local MoneyMaker = Tycoon.Foundation.Conveyor.MoneyMaker
local MoneyBlock = Tycoon.MoneyBlock
local owner = Tycoon.Owner.Value
local upgrader = Tycoon.Upgrader.MoneyUpgrader
local upgraded = false
local cash = game.Players[owner].leaderstats.Cash.Value

local function Upgraded(hitPart)

    local upgraded = true
    print("Upgraded!")

end

upgrader.Touched:connect(Upgraded)


local function AddMoney(hitPart)

    if upgraded == true then
        print("upgrade complete!")
        cash = cash + 10
        MoneyBlock:Destroy()

            if upgraded == false then
                print("no upgrade!")
                cash = cash + 5
                MoneyBlock:Destroy()

        end
    end 
end

MoneyMaker.Touched:connect(AddMoney)

Thanks for the help!

2 answers

Log in to vote
1
Answered by
amanda 1059 Moderation Voter
6 years ago

The reason that it is not working, is because of line 12.

Instead of overwriting the current upgraded variable, you create a new one in the local scope of that function.

To make it simple, remove local in that case, because the variable already exists.

After doing that, the if statement will pass because the variable was actually changed.

--

Next, implement what incapaz said. If you reference a property in a variable, it stores it's value, not the actual property.

So what you should do, is store the object itself, and only use the property inline to change it.

local block = script.Parent
local Tycoon = game.Workspace.Tycoon
local MoneyMaker = Tycoon.Foundation.Conveyor.MoneyMaker
local MoneyBlock = Tycoon.MoneyBlock
local owner = Tycoon.Owner.Value
local upgrader = Tycoon.Upgrader.MoneyUpgrader
local upgraded = false
local cash = game.Players[owner].leaderstats.Cash

local function Upgraded(hitPart)
    upgraded = true
    print("Upgraded!")
end
upgrader.Touched:Connect(Upgraded)

local function AddMoney(hitPart)
    if upgraded == true then
        print("upgrade complete!")
        cash.Value = cash.Value + 10
        MoneyBlock:Destroy()
        if upgraded == false then
            print("no upgrade!")
            cash.Value = cash.Value + 5
            MoneyBlock:Destroy()
        end
    end 
end
MoneyMaker.Touched:Connect(AddMoney)

Doing so should fix your initial problems. I am not completely sure of your system, but from here you can move on.

Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Line 8. Remove the .Value and add it to lines 24 and 29.

Line 8:

local cash = game:GetService("Players")[tostring(owner)].leaderstats.Cash

Line 24:

cash.Value = cash.Value + 10

Line 29:

cash.Value = cash.Value + 5
0
Thanks, but the if/else statement still won't execute for some reason CoosBuckett 10 — 6y

Answer this question