--DATA STORE--
local dataStoreService = game:GetService("DataStoreService") local mainDataStore = dataStoreService:GetDataStore("mainDataStore") game.Players.PlayerAdded:Connect(function(player) pcall(function() local leaderstats = Instance.new("Folder", player) leaderstats.Name = "leaderstats" local cash = Instance.new("IntValue", leaderstats) cash.Name = "Cash" cash.Value = mainDataStore:GetAsync(player.UserId) or 0 local subscribers = Instance.new("IntValue", leaderstats) subscribers.Name = "Subscribers" subscribers.Value = mainDataStore:GetAsync(player.UserId) or 0 print("Data Loaded!") cash.Changed:Connect(function() mainDataStore:SetAsync(player.UserId,player.leaderstats.Cash.Value,player.leaderstats.Subscribers.Value) print("Data Saved!") end) end) end) game.Players.PlayerRemoving:Connect(function(player) pcall(function() mainDataStore:SetAsync(player.UserId,player.leaderstats.Cash.Value,player.leaderstats.Subscribers.Value) print("Data Saved!") end) end)
I know I should be using updatasync etc, still learning..
--SERVER REMOTE--
game.Workspace.Part.ClickDetector.MouseClick:Connect(function(player) game.Workspace.Part.CashAdd:FireClient(player) end)
--CLIENT REMOTE--
local cashAdd = game.Workspace.Part.CashAdd local function cashAddFired(player) game.Players.LocalPlayer:WaitForChild("leaderstats").Cash.Value = game.Players.LocalPlayer:WaitForChild("leaderstats").Cash.Value + 1 end cashAdd.OnClientEvent:Connect(cashAddFired)
Improvements
Use GetService()
for the Players
Service
Use a table iteration to store your data rather than each individual value
Use workspace
instead of game.Workspace
Use WaitForChild() to make sure an instance exists before using it
Issues
Client Changes are not replicated to the Server as a result of FilteringEnabled, instead, since you are changing the value via a ClickDetector, all of the code here can remain on the server.
Revised Server Script #1
local dataStoreService = game:GetService("DataStoreService") local mainDataStore = dataStoreService:GetDataStore("mainDataStore") local function SaveData(player) local SaveArray = {} for index, value in pairs(player:WaitForChild("leaderstats"):GetChildren()) do SaveArray[value.Name] = value.Value end local success, result = pcall(function() mainDataStore:SetAsync(player.UserId, SaveArray) end) if not success then warn(result) else print("Data Saved!") end end game"GetService("Players").PlayerAdded:Connect(function(player) local leaderstats = Instance.new("Folder", player) leaderstats.Name = "leaderstats" local cash = Instance.new("IntValue", leaderstats) cash.Name = "Cash" cash.Value = 0 local subscribers = Instance.new("IntValue", leaderstats) subscribers.Name = "Subscribers" subscribers.Value = 0 local success, result = pcall(function() mainDataStore:GetAsync(player.UserId) end) if not success then warn(result) else local LoadArray = mainDataStore:GetAsync(player.UserId) if LoadArray then for index, value in pairs(leaderstats:GetChildren()) do value.Value = LoadArray[value.Name] end print("Data Load") else print("No Data") end end cash:GetPropertyChangedSignal("Value"):Connect(function() SaveData(player) end) end) game:GetService("Players").PlayerRemoving:Connect(function(player) SaveData(player) end)
Revised Server Script #2
workspace:WaitForChild("Part"):WaitForChild("ClickDetector").MouseClick:Connect(function(player) local cash = player:WaitForChild("leaderstats"):WaitForChild("Cash") cash.Value = cash.Value + 1 end)