I’m trying to make a game where once you collect items like tokens, the game will save your data once you leave.
I have a script in ServerScriptService and I’ve tried to create a few datastores and were all unsuccessful. It would always either make the leaderboard not show or not allow tokens to be collected.
I’ve tried to look at YouTube videos and work off of the Dev Hub but all was unsuccessful. It keeps ruining the other parts of the game.
Here is my script.
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new('Folder',player)
leaderstats.Name = 'leaderstats'
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
local Scripts = Instance.new('IntValue', leaderstats)
Scripts.Name = 'Scripts'
Scripts.Value = 0
local Tokens = Instance.new('IntValue', leaderstats)
Tokens.Name = 'Tokens'
Tokens.Value = 0
local data
local success, errormessage = pcall (function()
data = myDataStore:GetAsync(player.UserId.."-Scripts")
end
if success then
Scripts.Value = data
else
print("There was an error")
warn(errormessage)
end
end)
game.ReplicatedStorage.Remotes.Add.OnServerEvent:Connect(function(player, Ammount)
local currency = 'Scripts'
player.leaderstats[currency].Value = player.leaderstats[currency].Value + Ammount
local succes, errormessage = pcall(function()
myDataStore:SetAsync(player.UserId.."-Scripts",player.Leaderstates.Scripts.Value)
end)
if success then
print("Data Successfully Saved")
else
print("There was an error saving the data")
warn(errormessage)
end
end)
Any help would be appreciated. Thanks for reading.
Alright so the main problem with your script is that you are not writing the variables correctly
You wrote “Leaderstates” instead of “leaderstats” and this goes on for a lot of variables in the script
I have fixed all the variables and your script should work now
I also noticed that you save when the player gets money. I do not recommend this because of the datastore limits. There has to be a 6 second delay between every save with the same key (key = player.UserId…"-Scripts" in this script). If you do not follow the datastore limits you can experience dataloss or worse
Another thing is that you add money when the client calls the remoteEvent. This is not secure because hackers can spam this remote and give themselves millions. What i mean with this is whenever the client calls the server with the “Add” remote you will get x amount of money.
Since hackers can change and do EVERYTHING on the client they can spam this remote and give themselves money. (I recommend not giving money through client, try to only give money with serverscripts that are not controlled by the client like what you did here)
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new('Folder',player)
leaderstats.Name = 'leaderstats'
local Scripts = Instance.new('IntValue', leaderstats)
Scripts.Name = 'Scripts'
Scripts.Value = 0
local Tokens = Instance.new('IntValue', leaderstats)
Tokens.Name = 'Tokens'
Tokens.Value = 0
local data
local success, errormessage = pcall (function()
data = myDataStore:GetAsync(player.UserId.."-Scripts")
end)
if success then
Scripts.Value = data
else
print("There was an error")
warn(errormessage)
end
while wait(7) do
local succes, errormessage = pcall(function()
myDataStore:SetAsync(player.UserId.."-Scripts",player.leaderstats.Scripts.Value)
end)
if succes then
print("Data Successfully Saved")
else
print("There was an error saving the data")
warn(errormessage)
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local succes, errormessage = pcall(function()
myDataStore:SetAsync(player.UserId.."-Scripts",player.leaderstats.Scripts.Value)
end)
if succes then
print("Data Successfully Saved")
else
print("There was an error saving the data")
warn(errormessage)
end
end)
game.ReplicatedStorage.Remotes.Add.OnServerEvent:Connect(function(player, Ammount)
local currency = 'Scripts'
player.leaderstats[currency].Value = player.leaderstats[currency].Value + Ammount
end)
This script also takes care of the data limit issue
Hello, I’ve tried the script that you have provided me. And the same issue happened when I tried to create my datastore which was, the leaderstats don’t show up in-game, and collecting tokens which just ties in with the leaderstats not working.
I have tried to publish the game already and it didn’t work. It said in my token script that leaderstats is not a valid member of Players. And then it just brings me to line 9 in my token script