Tycoon money display not working in multiplayer

I am trying to make a tycoon in which you can collect money by stepping on a brick .In single player everything works as intended, but when I try to use the “Team test” feature the GUI stops displaying how much money you have to collect. I do not know what is causing this, in both multiplayer and single player collecting money works fine, but the sign always says 0 even if you have more than 0.

Script that destroys parts:

wait(0.5)
destroyPart = script.Parent
givemoney = game.ServerStorage.moneytogive
players = game.Players:GetPlayers()
print(players)
destroyPart.Touched:Connect(function(hit)
    print("h")
    if hit.BrickColor == BrickColor.new("Magenta") then
accessory and other parts in the map.
        local moneyToGive = hit:FindFirstChild("MoneyToGive")
           givemoney.Name = "moneytogive"
        givemoney.Value = givemoney.Value + moneyToGive.Value
        hit:Destroy()
    end
end)

script for part that gives money:

local ServerStorage = game:GetService("ServerStorage")
debounce = false

script.Parent.Touched:Connect(function(hit)
    wait()
    local moneyToGive = ServerStorage:FindFirstChild("moneytogive")
    local Character = hit.Parent
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    local Humanoid = Character:FindFirstChildOfClass("Humanoid")
    if debounce == false and Humanoid ~= nil and player.Team == game.Teams["Purple Tycoon"] then
        debounce = true
        player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + moneyToGive.Value
        moneyToGive.Value = 0
        wait(4)
        debounce = false
    end
end)

script for gui that displays how much money you have:

wait(1)
text = script.Parent
local ServerStorage = game:GetService("ServerStorage")
local money = ServerStorage:FindFirstChild("moneytogive")
money:GetPropertyChangedSignal("Value"):Connect(function()
    text.Text = money.Value
end) 

(btw this one is a localscript)

You can’t access ServerStorage from a Local Script, as far as I’m aware, unless you use a RemoteEvent/RemoteFunction that connects in a Server Script which is able to return the value.

Edit: From the code it only seems like you have a single “moneytogive” Number/IntValue. This would mean with more tycoons, each player would be able to step on the part and collect every bit of money. I also don’t know exactly how you have everything set-up but it seems that way. If I’m correct, you would want a different storage of that money for each tycoon.

Rather than access to the service, its the behaviour of the contents of ServerStorage that matters. The client can still access ServerStorage but the descendants aren’t replicated to the client, so using it is pointless. Things won’t run there either.

So how would I resolve this issue?

Ah, makes sense. I haven’t tried to use Local Scripts to connect with the ServerStorage because I figured they couldn’t access it, but I guess that makes sense in a way considering the server-client method.

In response to you, in the case that @colbert2677 mentioned on how ServerStorage doesn’t remplicate to the client, I believe using a RemoteFunction might be a possible solution. If you invoke the function from the client (PathToRemoteFunction:InvokeServer()) then you would be able to do something similar to this on the server:

PathToRemoteFunction.OnServerInvoke:Connect(function()
return game.ServerStorage.moneytogive.Value
end)

What this does it tells the server to retrieve the information then send it back to the client using “return”. I would consider using this or you could look into making it work with RemoteEvents, but I believe in your case using a RemoteFunction would be simpler and cleaner with the same result.

1 Like