You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
I want my Datastores to work.
What is the issue? Include screenshots / videos if possible!
The Datastores won’t work.
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I have made a separate variable for the player. And that is it. And I have looked for solutions on the Developer Hub. But gone worked.
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
Here is the code.
local ds = game:GetService("DataStoreService"):GetDataStore("CoinsDataStore")
local p = game:GetService("Players")
p.PlayerAdded:Connect(function(plr)
wait()
local plrkey = "id_"..plr.UserId
local s = {plr.leaderstats.Taps, plr.leaderstats.Gems, plr.leaderstats.Rebirths}
local sd = ds:GetAsync(plrkey)
if sd then
s[1] = sd
s[2] = sd
s[3] = sd
else
local nfs = {s[1].Value, s[2].Value, s[3].Value}
ds:GetAsync(plrkey, nfs)
end
print(sd)
end)
game.Players.PlayerRemoving:Connect(function(plr)
ds:SetAsync("id_"..plr.UserId, {plr.leaderstats.Taps.Value, plr.leaderstats.Gems.Value, plr.leaderstats.Rebirths.Value})
end)
You should probably wrap your functions in a pcall so you know if your code ran successfully or not. Shows you how to use them in a datastore here: Data Stores
local dss = game:GetService("DataStoreService")
local ds = dss:GetDataStore("CoinsDataStore")
local p = game:GetService("Players")
p.PlayerAdded:Connect(function(plr)
local leaderstats = plr:WaitForChild("leaderstats")
local taps = leaderstats:WaitForChild("Taps")
local gems = leaderstats:WaitForChild("Gems")
local rebirths = leaderstats:WaitForChild("Rebirths")
local data = ds:GetAsync("id_"..plr.UserId)
if data then
taps.Value = data[1] or 0
gems.Value = data[2] or 0
rebirths.Value = data[3] or 0
end
end)
p.PlayerRemoving:Connect(function(plr)
ds:SetAsync("id_"..plr.UserId, {plr.leaderstats.Taps.Value, plr.leaderstats.Gems.Value, plr.leaderstats.Rebirths.Value})
end)