Answered by
4 years ago Edited 4 years ago
As far as I understand, you are working very hard to avoid using RemoteEvents
. While this might work, it is going to be a nightmare to synchronize properly. Making shenanigans like this sooner or later turn your scripts into a spaghetti code.
While your script could be fixed, I will update it to use RemoteEvent
anyway. It would break for example when two players join at the same time. No more excuses, you need to start using Remotes
, or you will never make a fully functional game.
Local Script:
01 | local PayCheckEvent = game.ReplicatedStorage:WaitForChild( "PayCheckEvent" ) |
03 | local function OnPayCheckEvent(amount) |
04 | script.Parent.Text = "You've Received $" .. amount |
05 | script.Sound.Playing = true |
06 | script.Sound.Looped = false |
07 | script.Parent.Parent.Visible = true |
09 | script.Sound.Playing = false |
10 | script.Parent.Parent.Visible = false |
13 | PayCheckEvent.OnClientEvent:Connect(OnPayCheckEvent) |
ServerScript:
01 | local datastore = game:GetService( "DataStoreService" ) |
02 | local ds 1 = datastore:GetDataStore( "CashSaveSystem" ) |
04 | local PayCheckEvent = Instance.new( "RemoteEvent" ) |
05 | PayCheckEvent.Name = "PayCheckEvent" |
06 | PayCheckEvent.Parent = game.ReplicatedStorage |
10 | game.Players.PlayerAdded:Connect( function (plr) |
11 | local folder = Instance.new( "Folder" , plr) |
12 | folder.Name = "leaderstats" |
13 | local cash = Instance.new( "IntValue" , folder) |
16 | cash.Value = ds 1 :GetAsync(plr.UserId) or 500 |
17 | ds 1 :SetAsync(plr.UserId, cash.Value) |
19 | cash.Changed:connect( function () |
20 | ds 1 :SetAsync(plr.UserId, cash.Value) |
23 | local function PayCheck() |
24 | local teams = game:GetService( "Teams" ) |
26 | local payCheckTable = { |
31 | [ "HeavyGunDealer" ] = 225 , |
33 | [ "AdminOnDuty" ] = 10000 |
36 | for i, v in pairs (payCheckTable) do |
37 | if plr.Team = = teams [ i ] then |
43 | while wait( 55 ) and plr.Parent = = game.Players do |
44 | local paycheck = PayCheck() |
45 | cash.Value = cash.Value + paycheck |
46 | PayCheckEvent:FireClient(plr,paycheck) |
Hope this helps.
Edit: I changed player
to plr
in server script.