Setting values to data store save

Hey,
So I have this script:

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
local VSIZE = game.StarterGui.UI.VSIZE
local IQ = game.StarterGui.UI.IQ
local SIZE  = game.StarterGui.UI.SIZE
local STATCHANGE = script.Parent.STATCHANGE

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Coins = Instance.new("IntValue")
	Coins.Name = "Coins"
	Coins.Parent = leaderstats
	
	local Size = Instance.new("IntValue")
	Size.Name = "Size"
	Size.Parent = leaderstats
	
	local data
	local data2
	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(player.UserId.."-Coins")
		data2 = myDataStore:GetAsync(player.UserId.."-Size")
	end)
	
	if success then
		Coins.Value = data
		Size.Value = data2
	else
		print("loading error")
		warn(errormessage)
	end	
	
	
	game.ReplicatedStorage.HEADINCREASESTAT.OnServerEvent:Connect(function(player)
		Size.Value = Size.Value + game.StarterGui.UI.BOOK.Value
	end)
	
	game.ReplicatedStorage.SELLSTAT.OnServerEvent:Connect(function(player)
		Coins.Value = Coins.Value + Size.Value * IQ.Value
		Size.Value = 0
	end)
	
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId.."-Coins",player.leaderstats.Coins.Value)
		myDataStore:SetAsync(player.UserId.."-Size",player.leaderstats.Size.Value)
	end)
	
	if success then
		print("Success")
	else
		print("Saving error")
		warn(errormessage)
	end
end)

This code saves and loads leaderstats values and what am trying to do right now is setting int values in the starter GUI to the values that are saved, however I am having trouble with this,how would I do it?

Data stores can only work on server side. So local scripts can do this.

You would need to add in a remote event or some kind of communication between the local scripts and the server script.

Remote Events:

2 Likes

Minimic2002 is right if your going to use data save u have to set the values to the server side with remote events and data storage only works in the server side and if u need help of how to do it please message me and ill help!

remote events:Custom Events and Callbacks | Documentation - Roblox Creator Hub