Hey I’m making a DataStore script and when I leave and rejoin in roblox it isn’t working. Here’s the script
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("TimeData")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Minutes = Instance.new("IntValue")
Minutes.Name = "Minutes"
Minutes.Parent = leaderstats
local data
local success, errormessage = pcall(function()
data = dataStore:GetAsync(player.UserId.."-minutes")
end)
if success then
Minutes.Value = data
else
print("Error")
warn(errormessage)
end
while task.wait(1) do
Minutes.Value += 1
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local playeruserId = "Player_"..player.UserId
local data = {
Minutes = player.leaderstats.Minutes.Value
}
local success, errormessage = pcall(function()
dataStore:SetAsync(playeruserId, data)
end)
if success then
print("Successfuly saved data!")
else
print("There was an error in saving!")
warn(errormessage)
end
end)
I see that you are loading data. I suggest you remove the .UserId…"-minutes". if success then Minutes.Value = data: You need to have a saving code. Don’t forget, you have to save data to load data. In the if statement above, you are basically setting the value of Minutes to your player ID. What you need to do instead is make a script that saves, and have a table called Data. In there, you must add a Minutes value and set it to the leaderstats minutes value. I will give you a code example below if that helps.
local datastoreservice = game:GetService("DataStoreService")
local datastore = datastoreservice:GetDataStore("datastore")
game.Players.PlayerRemoving:Connect(function(player)
local playeruserId = "Player_"..player.UserId
local data = {
Minutes = player.leaderstats.Minutes.Value
}
local success, errormessage = pcall(function()
datastore:SetAsync(playeruserId, data)
end)
if success then
print("Successfuly saved data!")
else
print("There was an error in saving!")
warn(errormessage)
end
end)
This right here is the saving code. As you can see, there is a data table. Inside, is the value of Minutes, with its leaderstats value. What you need to do is go to your loading code, and put if success then Minutes.Value = data.Minutes If this doesn’t work, please let me know. Copy the code above into below your playeradded function.
I suggest you put the leaderstats and Minutes variables inside playeradded. When I test with 3 players, only 1 player’s number is counter while the other two got nothing except a blank intvalue. Also the while loop inside the playeradded.
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("TimeData")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Minutes = Instance.new("IntValue")
Minutes.Name = "Minutes"
Minutes.Parent = leaderstats
local data
local success, errormessage = pcall(function()
data = dataStore:GetAsync(player.UserId.."-minutes")
end)
if success then
Minutes.Value = data
else
print("Error")
warn(errormessage)
end
while task.wait(60) do
Minutes.Value += 1
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local success, errormessage = pcall(function()
dataStore:SetAsync(plr.UserId.."-minutes", plr.leaderstats.Minutes.Value)
end)
if success then
print("Succesfully Saved Players Data!")
else
print("Error Saving Player Data!")
warn(errormessage)
end
end)
Do you have DataStoreService enabled in Roblox Studio? If not then enable it, because that might be the issue. Also run this script in the command bar to set the datastore for testing. If it loads it, then your good.
local DataStoreService = game:GetService("DataStoreService")
local WinsLeaderboard = DataStoreService:GetDataStore("TimeData")
WinsLeaderboard:SetAsync("1423303857-minutes",2034850)
Have you tried using DataStore2? Seems like what you try to save is very simple, so DataStore2 would be optional for you, if you do not have much experience with DataStores
Have you tried forcefully setting your datastore and see if it can load it?
local DataStoreService = game:GetService("DataStoreService")
local WinsLeaderboard = DataStoreService:GetDataStore("TimeData")
WinsLeaderboard:SetAsync("1423303857-minutes",2034850)