Im working on datastores for the first time but I have some unanswered questions.
here is my datastore script:
local plr = game.Players:FindFirstChild(script.Parent.Name)
if not plr then
repeat wait() until plr
end
local datastore = game:GetService("DataStoreService")
local mydata = datastore:GetDataStore("mydata")
local value = script.Parent.Gems
value.Changed:Connect(function()
local success, fail = pcall(function()
mydata:SetAsync(plr.UserId.."Gems",value.Value)
end)
if success then
print("data saved")
else
print("an error has occured while saving")
warn(fail)
end
end)
game.Players.PlayerAdded:Connect(function(player)
local data
local success, fail = pcall(function()
data = mydata:GetAsync(player.UserId.."Gems")
end)
if success then
value.Value = data
else
print("data load failed")
warn(fail)
end
end)
the script starts in the character but then relocates to serverscriptservice. I put it there so data can be safe. but I ran into a problem, I need a client side gui to read off of the value “Gem(s)”
but because its in serverscriptservice the client cant see it. if I put it in a place like the workspace its vulnerable to attackers.
also, the script has not been tested yet so if there are any errors please let me know.
So this is an opportunity to start understanding the server/client model.
Even though you have this server script inside the character, the player(client) cannot access the data because the code is being executed on the server. So obviously you need a way for players/server to communicate and the answer to that is RemoteEvents/RemoteFunctions.
In your case you’d want something along the lines of
Player joins game and server loads data
Player local scripts execute and request data from server using remote function
Server retrieves data and returns data to client
Client displays data in some graphical format.
Edit: This is just a quick short version to give you some ideas to research on your own, there are tons of tutorials on the forums that do a much better job explaining than I ever could.
so I got it working with one remote function in replicatedstorage,but all clients will be requesting from that remote function every 0.5 seconds. will this overload the remote function or can it handle many requests at the same time?
In my opinion, the best way to handle a player seeing his data is to place the values somewhere in ReplicatedStorage so you can see the data without having to use RemoteFunctions.
The only thing wrong with this model, though, is that exploiters can see other players’ data. If that’s something you’re fine with, go ahead and put it in ReplicatedStorage.
If you don’t want other players to see everyone’s data, I’d understand, but you should know that even if they can see the data, they can’t change it at all. The “changes” they make are only client sided for the exploiter, and for everyone else the data remains the same.
Many games run 100+ remote event/function calls a second. But in this case that is extremely wasteful. A better way would be to have the server fire a remote event only when there is a change in your data set.