Hello. Today I tried creating a system for my game that saves each player's high score. W/o a further a do, I'll tell that I ended up with this code...

Local script inside of StarterPlayerScripts

--//Variables
local player = game.Players.LocalPlayer

local repStorage = game:GetService("ReplicatedStorage")
local remoteEvent = repStorage:WaitForChild('HighScore')
local dataStore = game:GetService('DataStoreService'):GetDataStore('PlayerStatsStore')

local dataKey = player.UserId --Data key to access player data

local leaderstats = player:WaitForChild('leaderstats')
local highScore = leaderstats:WaitForChild('High Score')
local highScoreValue = highScore.Value

--//The loop fires server 'each second'
while true do	
	--Firing server
	if highScoreValue.Changed then
		remoteEvent:FireServer()
	end
	wait(1)
end

Server script in ServerScriptService

--//Variables
local repStorage = game:GetService("ReplicatedStorage")
local remoteEvent = repStorage:WaitForChild('HighScore')
local dataStore = game:GetService('DataStoreService'):GetDataStore('PlayerStatsStore')

--//Server fired 
remoteEvent.OnServerEvent:Connect(function(player)
	--//Local variables
	local dataKey = player.UserId --Data key to access player data

	local leaderstats = player:WaitForChild('leaderstats')
	local highScore = leaderstats:WaitForChild('High Score')
	local highScoreValue = highScore.Value
	
	local function savingStats() --Saving player high score function
		local statsToSave = highScoreValue
		dataStore:SetAsync(dataKey, statsToSave)
	end
	
	savingStats()

	print(player.Name .. ' has saved his high score') --Making sure it works; it doesn't though
end)

I'm just so dumbheaded about this. There is a remote event inside of RepStorage that allows the local script to communicate with server script; and when the server script gets triggered, it accesses that player's datastore and changes that player's stored high score. Dev console prints this problem on the client side: "Datastores cannot be accessed from client". Help??

2 comments

0

Do not use the Datastore on the localscript, it's that simple.

0

What do I do then? Any suggestions?