Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Data Saves when changed from server, but not from remote events?

--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)

2 comments

0

Changes on the Client will not replicate to the Server, and thus cannot be saved in a DataStore, any changes you want saved should be made on the Server

0

So whats the fix @SeperntineKing

1 answer

✓ Accepted answer

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.