FireAllClients and OnClientEvent not reciprocating correctly

Hi.
I’m working on a tower defense game, and so far, it’s going great! Only one problem at the moment, however: paying out players.
Each player should receive a bonus sum of cash for each wave. Because of how I set up everything, the player’s Cash value is in PlayerScripts, which works for GUIs. Unfortunately, I have a problem with getting payouts to cross between the scripts.

In my main script, the WaveComplete function pays the players the amount of cash “cash” via FireAllClients. In the LocalScript, it adds to the CashValue (the LocalScript is in StarterPlayerScripts) the “cash” argument.

The main script:

local function nextwave(cash)
	game.Workspace.WaveNumber.Value += 1
	game.Workspace.PayoutEvent:FireAllClients(cash)
	print(cash)
end

The localscript:

game.Workspace.PlacementEvent.OnClientEvent:Connect(function(cash)
	print(cash)
	script.Parent.Cash.Value += cash
end)

However, while the next line prints the correct cash value in the main script, nothing comes back from the LocalScript. The cash value is not added to, and the print() function does not run. It’s like it doesn’t even receive it.

Is there something I’m missing? Am I slowly going insane from programming this game and it’s a really stupid mistake or simple solution?

AAAAAnd I’m stupid. I called the wrong event. I cannot believe… wow.

Was about to reply with that… I would migrate your stats to the player with leaderstats!

That’s a good idea, though! I’ve never experimented with leaderstats before myself, so I’ll give it a shot.

Try using:

local function buildLeaderstats(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local cash = Instance.new("IntValue")
    cash.Name = "Cash"   
    cash.Value = cashValue -- whatever value your cash is hooked up to
    cash.Parent = leaderstats
end

game:GetService("Players").PlayerAdded:Connect(buildLeaderstats)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.