Hello fellow developers! I’m having a problem with my datastore.
When I click and stuff it works, but when I leave it doesn’t save even though it says it does
Then it doesn’t save
Here’s my script
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Clicks = Instance.new("NumberValue")
Clicks.Name = "Clicks"
Clicks.Parent = leaderstats
local Rebirths = Instance.new("NumberValue")
Rebirths.Name = "Rebirths"
Rebirths.Parent = leaderstats
local data
local success, errormessage = pcall(function()
data = DataStore:GetAsync(player.UserId.."-clicks", player.UserId.."-rebirths")
end)
if success then
Clicks.Value = data
Rebirths.Value = data
else
print("There was an error")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
DataStore:SetAsync(player.UserId.."-clicks", player.leaderstats.Clicks.Value)
DataStore:SetAsync(player.UserId.."-rebirths", player.leaderstats.Rebirths.Value)
end)
if success then
print("Players data has been saved")
else
print("Players data is errored")
warn(errormessage)
end
end)
I took the reduced :GetAsync calls into account while I was writing my reply, but I’m assuming that his game is already released and may have previously saved data. Though if it isnt released, saving it as a table would be a better option.
Make sure you have Enable Studio Access to API Services enabled in the game settings so you can access data stores.
Also, adding on to what undiscvrd said earlier,
you should also have data1 and data2, or replace their names for clicks and rebirths, like this
local clicks
local rebirths
then change
data = DataStore:GetAsync(player.UserId.."-clicks", player.UserId.."-rebirths")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Clicks = Instance.new("NumberValue")
Clicks.Name = "Clicks"
Clicks.Parent = leaderstats
local Rebirths = Instance.new("NumberValue")
Rebirths.Name = "Rebirths"
Rebirths.Parent = leaderstats
local clicks = nil
local rebirths = nil
local success, errormessage = pcall(function()
clicks = DataStore:GetAsync(player.UserId.."-clicks")
rebirths = DataStore:GetAsync(player.UserId.."-rebirths")
end)
if success then
Clicks.Value = clicks
Rebirths.Value = rebirths
else
print("There was an error")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
DataStore:SetAsync(player.UserId.."-clicks", player.leaderstats.Clicks.Value)
DataStore:SetAsync(player.UserId.."-rebirths", player.leaderstats.Rebirths.Value)
end)
if success then
print("Players data has been saved")
else
print("Players data is errored")
warn(errormessage)
end
end)
Yes, clicks need to be fired from a remote event to the server, you can also validate the clicks there eventually, then change the leaderstats value from a script preferably in the serverscriptservice. Same thing with the rebirths. Never rely on the client for data validation.
tl;dr the scripts usually should be in server script service
local debounce = false
local debouncetime = .1
script.Parent.MouseButton1Click:Connect(function()
if not debounce then
debounce = true
local player = game.Players.LocalPlayer
local clicks = player:WaitForChild("leaderstats"):WaitForChild("Clicks")
local rebirths = player:WaitForChild("leaderstats"):WaitForChild("Rebirths")
local amount = script.Parent.Amount
local Multiplier = script.Parent.Multiplier
script.Parent:TweenSize(UDim2.new(0, 117,0, 118), Enum.EasingDirection.InOut, Enum.EasingStyle.Linear, .01)
wait(.02)
script.Parent:TweenSize(UDim2.new(0, 130,0, 131), Enum.EasingDirection.InOut, Enum.EasingStyle.Linear, .01)
clicks.Value += amount.Value + rebirths.Value
wait(debouncetime)
debounce = false
end
end)
No that wouldn’t work because even though the data is being changed on the client, other players and the server, won’t see that data. So you should have a RemoteEvent or, better yet, a RemoteFunction
If you don’t know what those are, they just send signals to the server or client so they can process the data. They can do server → client and client → server. RemoteFunctions also send a signal, but they require a response using return
Also, data that is verified on the client is easily exploited. That’s why a script that validates the data serverside is so much safer.