I made a datastore script, but everytime i rejoin it adds the extra coins and rebirths to the default values, and i just keep getting 228 coins and 2 rebirths everytime i join, it doesn’t save it.
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("TestData")
local repStorage = game:GetService("ReplicatedStorage")
local dataCache = {}
game.Players.PlayerAdded:Connect(function(player)
local success, data = pcall(function()
dataStore:GetAsync(player.UserId)
end)
if success then
if not data then
data = {
Coins = 100,
Rebirths = 1,
Gems = 0,
Pets = "",
Level = 1,
XP = 0,
Stage = 1
}
end
dataCache[player.UserId] = data
local screenGui = player.PlayerGui:WaitForChild("ScreenGui")
-- Set player gui
screenGui:WaitForChild("Coins"):WaitForChild("TextLabel").Text = data.Coins
screenGui:WaitForChild("Gems"):WaitForChild("TextLabel").Text = data.Gems
screenGui:WaitForChild("Level"):WaitForChild("Level").Text = data.Level
screenGui:WaitForChild("Rebirths"):WaitForChild("TextLabel").Text = data.Rebirths
else
warn("Failed to load data")
player:Kick("Error loading data, try again later")
end
dataCache[player.UserId].Coins = dataCache[player.UserId].Coins + 128
dataCache[player.UserId].Rebirths = dataCache[player.UserId].Rebirths + 1
print(dataCache[player.UserId])
end)
game.Players.PlayerRemoving:Connect(function(player)
print(dataCache[player.UserId])
local success, err = pcall(function()
dataStore:SetAsync(player.UserId, dataCache[player.UserId])
end)
dataCache[player.UserId] = nil
end)
repStorage.Remotes.RequestData.OnServerInvoke = function(player)
return dataCache[player.UserId]
end
Here, this will probably solve your issue. I’ve removed some stuff in your script that I found to be unnecessary, though you can simply re-add stuff into it.
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("TestData")
local repStorage = game:GetService("ReplicatedStorage")
local dataCache = {}
game.Players.PlayerAdded:Connect(function(player)
local data = dataStore:GetAsync(player.UserId)
if not data then
local newData = {
Coins = 100,
Rebirths = 1,
Gems = 0,
Pets = "",
Level = 1,
XP = 0,
Stage = 1
}
dataCache[player.UserId] = newData;
-- I really think this isn't a great way to set the UI whatsoever.. Perhaps use a local script and use a RemoteFunction w/ InvokeServer() to get the data and set the text within the UI that way.
screenGui:WaitForChild("Coins"):WaitForChild("TextLabel").Text = newData.Coins
screenGui:WaitForChild("Gems"):WaitForChild("TextLabel").Text = newData.Gems
screenGui:WaitForChild("Level"):WaitForChild("Level").Text = newData.Level
screenGui:WaitForChild("Rebirths"):WaitForChild("TextLabel").Text = newData.Rebirths
else
dataCache[player.UserId] = data;
end
print(dataCache[player.UserId])
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, err = pcall(function()
dataStore:SetAsync(player.UserId, dataCache[player.UserId])
end)
local removeInd = table.find(dataCache, player.UserId);
table.remove(dataCache, removeInd)
end)
repStorage.Remotes.RequestData.OnServerInvoke = function(player)
return dataCache[player.UserId]
end
screenGui isnt defined, the pcalls in getasync could cause the script to break in case roblox servers have an error, also not sure why the table find was usefull, either way it just doesnt work. Also you also made it that the gui only updates when theres no data, but nothing happens when there is
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("TestData")
local repStorage = game:GetService("ReplicatedStorage")
local dataCache = {}
game.Players.PlayerAdded:Connect(function(player)
local data;
local success, err = pcall(function()
data = dataStore:GetAsync(player.UserId)
end)
if not data and success then
local newData = {
Coins = 100,
Rebirths = 1,
Gems = 0,
Pets = "",
Level = 1,
XP = 0,
Stage = 1
}
dataCache[player.UserId] = newData;
-- I really think this isn't a great way to set the UI whatsoever.. Perhaps use a local script and use a RemoteFunction w/ InvokeServer() to get the data and set the text within the UI that way.
local screenGui = game.ScreenGui;
screenGui:WaitForChild("Coins"):WaitForChild("TextLabel").Text = newData.Coins
screenGui:WaitForChild("Gems"):WaitForChild("TextLabel").Text = newData.Gems
screenGui:WaitForChild("Level"):WaitForChild("Level").Text = newData.Level
screenGui:WaitForChild("Rebirths"):WaitForChild("TextLabel").Text = newData.Rebirths
elseif data and success then
dataCache[player.UserId] = data;
local screenGui = game.ScreenGui;
screenGui:WaitForChild("Coins"):WaitForChild("TextLabel").Text = data.Coins
screenGui:WaitForChild("Gems"):WaitForChild("TextLabel").Text = data.Gems
screenGui:WaitForChild("Level"):WaitForChild("Level").Text = data.Level
screenGui:WaitForChild("Rebirths"):WaitForChild("TextLabel").Text = data.Rebirths
end
print(dataCache[player.UserId])
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, err = pcall(function()
dataStore:SetAsync(player.UserId, dataCache[player.UserId])
end)
local removeInd = table.find(dataCache, player.UserId);
table.remove(dataCache, removeInd)
end)
repStorage.Remotes.RequestData.OnServerInvoke = function(player)
return dataCache[player.UserId]
end
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("TestData")
local repStorage = game:GetService("ReplicatedStorage")
local dataCache = {}
game.Players.PlayerAdded:Connect(function(player)
local data = nil
local success, err = pcall(function()
data = dataStore:GetAsync(player.UserId)
end)
if success then
if not data then
data = {
Coins = 100,
Rebirths = 1,
Gems = 0,
Pets = "",
Level = 1,
XP = 0,
Stage = 1
}
end
dataCache[player.UserId] = data
print(data)
else
warn("Failed to load data".. err)
return player:Kick("Error loading data, try again later")
end
dataCache[player.UserId].Coins = dataCache[player.UserId].Coins + 128
dataCache[player.UserId].Rebirths = dataCache[player.UserId].Rebirths + 1
print(dataCache[player.UserId])
end)
game.Players.PlayerRemoving:Connect(function(player)
print(dataCache[player.UserId])
local success, err = pcall(function()
dataStore:SetAsync(player.UserId, dataCache[player.UserId])
end)
dataCache[player.UserId] = nil
end)