I’m receiving this error when more of one player is in a server, here is the code:
local DataStoreService = game:GetService("DataStoreService")
game.Players.PlayerAdded:Connect(function(player)
local Effects = Instance.new("Folder",player)
Effects.Name = "Effects"
local TrailEquipped = Instance.new("StringValue",player)
TrailEquipped.Name = "TrailEquipped"
TrailEquipped.Value = ""
local ParticleEquipped = Instance.new("StringValue",player)
ParticleEquipped.Name = "ParticleEquipped"
ParticleEquipped.Value = ""
for i,v in pairs(game.ReplicatedStorage.Effects:WaitForChild("Trails"):GetChildren()) do
local Trail = Instance.new("BoolValue",Effects)
Trail.Name = v.Name
Trail.Value = DataStoreService:GetDataStore(v.Name):GetAsync(player.userId)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
for i,v in pairs(game.ReplicatedStorage.Effects:WaitForChild("Trails"):GetChildren()) do
DataStoreService:GetDataStore(v.Name):SetAsync(player.userId,player.Effects:WaitForChild(v.Name).Value)
end
end)
I recommend to have both player removing and a timer, as if it fails to save on removing then that entire session is gone. Somewhere between 60-120 seconds is a good amount that won’t cause queues.
Instead of saving a number or string, you save a table that contains multiple things.
So instead of my having a coin datastore, and an experience datastore, I have a data datastore, which is like
{
["Coins"] = 0;
["Exp"] = 0;
}
Then you basically use datastores as normal, with this table.
Now, for your case
You have an individual datastore for each effect, when you really only need a table of all the effects. You can use i,v in pairs to insert all the effects into a table, and the value the effect like:
local DataTable = {}
for i,v in pairs(game.ReplicatedStorage.Effects:WaitForChild("Trails"):GetChildren()) do
DataTable[v.Name] = player.Effects:FindFirstChild(v.Name).Value
end
local Main = DataStoreService:GetDataStore("Main")
--loading data
local DataTable = Main:GetAsync() or {} --if there is no data, use {} which is an empty table
--saving data
Data:SetAsync(DataTable)
Works the exact same as strings and numbers, just with tables.
Thank you for the help! I never used tables before, so thank you very much!
The final code is this:
local DataStoreService = game:GetService("DataStoreService")
local Main = DataStoreService:GetDataStore("Main")
game.Players.PlayerAdded:Connect(function(player)
local effects = Instance.new("Folder",player)
effects.Name = "Effects"
local DataTable = Main:GetAsync(player.userId)
if DataTable then
for i,v in pairs(game.ReplicatedStorage.Effects:WaitForChild("Trails"):GetChildren()) do
local Trail = Instance.new("BoolValue",effects)
Trail.Name = v.Name
Trail.Value = DataTable[v.Name]
end
else
for i,v in pairs(game.ReplicatedStorage.Effects:WaitForChild("Trails"):GetChildren()) do
local Trail = Instance.new("BoolValue",effects)
Trail.Name = v.Name
Trail.Value = false
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local DataTable = {}
for i,v in pairs(game.ReplicatedStorage.Effects:WaitForChild("Trails"):GetChildren()) do
DataTable[v.Name] = player.Effects:FindFirstChild(v.Name).Value
wait(1)
end
Main:SetAsync(player.userId, DataTable)
end)