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

Changed function not working?

Asked by
Scerzy 85
9 years ago

So I'm editing berezaa's tycoon model as a way to study and improve my scripting, and One thing I'm trying to do is add another form of currency called "crystals". So I went through his tycoon kit and mirrored everything he did with his money currency so that two currencies now exist. Now the way the script works is it has a leaderboard with "fake money", its only there to convey to the player how much money they have. The ACTUAL money they have is kept in the serverstorage. Here are snippets of the scripts I mirrored. The SECOND one is the one that isn't working. The other is there for reference.

game.Players.PlayerAdded:connect(function(player)
    local cashmoney = Instance.new("NumberValue",game.ServerStorage.MoneyStorage)
    local crystalmoney = Instance.new("NumberValue",game.ServerStorage.MoneyStorage)
    cashmoney.Name = player.Name
    crystalmoney.Name = player.Name.."2"
    local isowner = Instance.new("BoolValue",cashmoney)
    local isowner2 = Instance.new("BoolValue",crystalmoney)
    isowner.Name = "OwnsTycoon"
    isowner2.Name = "OwnsTycoon"
end)
game.Players.PlayerRemoving:connect(function(player)
    local cashmoney = game.ServerStorage.MoneyStorage:FindFirstChild(player.Name)
    local crystalmoney = game.ServerStorage.MoneyStorage:FindFirstChild(player.Name.."2")
    if cashmoney ~= nil then
        cashmoney:Destroy()
    end
    if crystalmoney ~= nil then
        crystalmoney:Destroy()
    end

And here's the problematic one

        local cash = Instance.new("IntValue")
        cash.Name = "Cash"
        cash.Value = 0

        local crystals = Instance.new("IntValue")
        crystals.Name = "Crystals"
        crystals.Value = 0

        local cashmoney = game.ServerStorage.MoneyStorage:FindFirstChild(newPlayer.Name)
        if cashmoney ~= nil then
            cashmoney.Changed:connect(function()
                cash.Value = cashmoney.Value
            end)
        end

        local crystalmoney = game.ServerStorage.MoneyStorage:FindFirstChild(newPlayer.Name.."2")
        if crystalmoney ~= nil then
            print("hi")
            crystalmoney.Changed:connect(function()
                crystals.Value = crystalmoney.Value
            end)
        end

        cash.Parent = stats
        crystals.Parent = stats

Now with the cash currency, if I change the value of the leaderboard, the value inside the server storage also changes. and vice versa. However, with the crystal currency, if I change the value of the leaderboard, the value inside the serverstorage DOES NOT change. But if I change the value in the serverstorage, the crystal value in the leaderboard DOES change. How do I make it so that changing the value in the leaderboard also changes the value in the server storage? I thought I got it down by using the Changed function.

P.S: the output prints hi, so I know crystalmoney isn't nil

1 answer

Log in to vote
0
Answered by 8 years ago

The Changed event is not working because it is located in a loop. A loop finishes so quickly that the event will probably never get fired.

Also at line 02, you try to create a new instance. However, the problem is: in Lua the line automatically ends, meaning you should locate the brackets straight after Instance.new.

Ad

Answer this question