Changed function not working?
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.
01 | game.Players.PlayerAdded:connect( function (player) |
02 | local cashmoney = Instance.new( "NumberValue" ,game.ServerStorage.MoneyStorage) |
03 | local crystalmoney = Instance.new( "NumberValue" ,game.ServerStorage.MoneyStorage) |
04 | cashmoney.Name = player.Name |
05 | crystalmoney.Name = player.Name.. "2" |
06 | local isowner = Instance.new( "BoolValue" ,cashmoney) |
07 | local isowner 2 = Instance.new( "BoolValue" ,crystalmoney) |
08 | isowner.Name = "OwnsTycoon" |
09 | isowner 2. Name = "OwnsTycoon" |
11 | game.Players.PlayerRemoving:connect( function (player) |
12 | local cashmoney = game.ServerStorage.MoneyStorage:FindFirstChild(player.Name) |
13 | local crystalmoney = game.ServerStorage.MoneyStorage:FindFirstChild(player.Name.. "2" ) |
14 | if cashmoney ~ = nil then |
17 | if crystalmoney ~ = nil then |
18 | crystalmoney:Destroy() |
And here's the problematic one
01 | local cash = Instance.new( "IntValue" ) |
05 | local crystals = Instance.new( "IntValue" ) |
06 | crystals.Name = "Crystals" |
09 | local cashmoney = game.ServerStorage.MoneyStorage:FindFirstChild(newPlayer.Name) |
10 | if cashmoney ~ = nil then |
11 | cashmoney.Changed:connect( function () |
12 | cash.Value = cashmoney.Value |
16 | local crystalmoney = game.ServerStorage.MoneyStorage:FindFirstChild(newPlayer.Name.. "2" ) |
17 | if crystalmoney ~ = nil then |
19 | crystalmoney.Changed:connect( function () |
20 | crystals.Value = crystalmoney.Value |
25 | 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