The DataStore works but I have no idea on how to load the saved player data when they re-join the game.
This is the datastore:
local ClickDataStore = DataStoreService:GetDataStore("ClickDataStore")
game.Players.PlayerAdded:Connect(function(player)
local stats = Instance.new("Folder")
stats.Name = "leaderstats"
stats.Parent = player
local Clicks = Instance.new("IntValue")
Clicks.Name = "Clicks"
Clicks.Parent = stats
local data
local success, errormessage = pcall(function()
data = ClickDataStore:GetAsync(player.userId.."Clicks")
end)
if success then
Clicks.Value = data
print("Player Data successfully saved!")
else
print("There was an error when saving data")
warn(errormessage)
end
while wait(60) do
local success, error = pcall(function()
ClickDataStore:SetAsync(player.UserId.."Clicks", Clicks.Value)
end)
if success then
print("Data auto saved")
else
print("There was an error when saving data")
warn(error)
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
ClickDataStore:SetAsync(player.UserId.."Clicks",player.leaderstats.Clicks.Value)
end)
if success then
print("Player Data successfully saved!")
else
print("There was an error when saving data")
warn(errormessage)
end
end)
I am confused on what you mean by this. The PlayerAdded event should fire and have GetAsync() retrieve your data when a player joins in the game. Check out this devhub on Datastores if you are still confused, it should give you answers on Datastores.
As much I would like to help, it would be better to learn on your own. What I could say is that you don’t need to put a loop there, instead outside of it.
local ClickDataStore = DataStoreService:GetDataStore("ClickDataStore")
game.Players.PlayerAdded:Connect(function(player)
local stats = Instance.new("Folder")
stats.Name = "leaderstats"
stats.Parent = player
local Clicks = Instance.new("IntValue")
Clicks.Name = "Clicks"
Clicks.Parent = stats
local data
local success, errormessage = pcall(function()
data = ClickDataStore:GetAsync(player.userId.."Clicks")
end)
if success then
Clicks.Value = data
print("Player Data successfully saved!")
else
print("There was an error when saving data")
warn(errormessage)
end
spawn(function()
while wait(60) do
local success, error = pcall(function()
ClickDataStore:SetAsync(player.UserId.."Clicks", Clicks.Value)
end)
if success then
print("Data auto saved")
else
print("There was an error when saving data")
warn(error)
end
end)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
ClickDataStore:SetAsync(player.UserId.."Clicks",player.leaderstats.Clicks.Value)
end)
if success then
print("Player Data successfully saved!")
else
print("There was an error when saving data")
warn(errormessage)
end
end)
If you ask most devs whats the most anoiying thing about scripting they usually say Datastores, if you do them right you have little to no issues but that takes alot of checking and making sure things are loaded and doing what they should be doing aside from that since your new I suggest learning basics first since a datastore that won’t eventually wipe peoples data takes a while to lear, that being said your datastore would fail and wipe not save data early on just for the pure fact of you doing a loop that saves every 60 seconds, saving every certain amount of time will overload the datastore max limit and start queing data then it will stop responding so you shouldn’t be autosaving in the way your doing it but to answer your question to stop the loop when they leave you do
local KeepMyLoopRunning[Player] = {}
if KeepMyLoopRunning[Player] == nil then KeepMyLoopRunning[Player] = true end
while true do
wait(60)
if KeepMyLoopRunning[Player] = true then
--Do Something like save data or whatever you want
else
Break
end
end
game.Players.PlayerRemoving:Connect(function(Player)
KeepMyLoopRunning[Player] = false
end)
so your saying if the that value is true then keep running the loop but whenever it’s set to false then you break the loop and make the loop stop running. the [Player] part is so the game knows on the server to only have this value for this individual player and not for everyone so you start it as a table then say if it’s nil wich it starts as nil then to make it equal to true so the loop can start running until you make it false and the loop breaks.
local DataStoreService = game:GetService("DataStoreService")
local ClickDataStore = DataStoreService:GetDataStore("ClickDataStore")
game.Players.PlayerAdded:Connect(function(player)
local stats = Instance.new("Folder")
stats.Name = "leaderstats"
stats.Parent = player
local Clicks = Instance.new("IntValue")
Clicks.Name = "Clicks"
Clicks.Parent = stats
local data
local success, errormessage = pcall(function()
data = ClickDataStore:GetAsync(player.userId.."Clicks")
end)
if success then
Clicks.Value = data
print("Player Data successfully saved!")
else
print("There was an error when saving data")
warn(errormessage)
end
spawn(function()
while wait(60) do
local success, error = pcall(function()
ClickDataStore:SetAsync(player.UserId.."Clicks", Clicks.Value)
end)
if success then
print("Data auto saved")
else
print("There was an error when saving data")
warn(error)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
ClickDataStore:SetAsync(player.UserId.."Clicks",player.leaderstats.Clicks.Value)
end)
if success then
print("Player Data successfully saved!")
else
print("There was an error when saving data")
warn(errormessage)
end
end)