I’m trying to save a lot of bool values at a time. But it’s crashing
I got this script and modified it from the dev forum
Please tell me what I’m doing wrong, Thanks!
local DataStoreService = game:GetService("DataStoreService");
local DataStore = DataStoreService:GetDataStore("RopeService");
game.Players.PlayerAdded:Connect(function(player)
for _, ropeBadge in pairs(workspace.Badges:GetChildren()) do
if ropeBadge:IsA("StringValue") then
local boolval = Instance.new("BoolValue", player)
boolval.Name = ropeBadge.Value
boolval.Value = DataStore:GetAsync(player.UserId) or false
if boolval.Value == true then
print(ropeBadge.Value .. " is owned by " .. player.Name)
end
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
for _, ropeBadge in pairs(workspace.Badges:GetChildren()) do
if ropeBadge:IsA("StringValue") then
coroutine.resume(coroutine.create(
function()
DataStore:SetAsync(player.UserId, player:FindFirstChild(ropeBadge.Value).Value)
end
)
)
end
end
end)
You are requesting “GetAsync” way too many times. Since it is all the same value, just define a variable that is equal to “DataStore:GetAsync(player.UserId) or false” on line 5 and just set boolval.Value to that variable.
i tried to do that and it still shows the “Not running script because past shutdown deadline (x37)” error
am i doing it right?
local DataStoreService = game:GetService("DataStoreService");
local DataStore = DataStoreService:GetDataStore("RopeService");
game.Players.PlayerAdded:Connect(function(player)
local a = DataStore:GetAsync(player.UserId) or false
for _, ropeBadge in pairs(workspace.Badges:GetChildren()) do
if ropeBadge:IsA("StringValue") then
local boolval = Instance.new("BoolValue", player)
boolval.Name = ropeBadge.Value
boolval.Value = a
if boolval.Value == true then
print(ropeBadge.Value .. " is owned by " .. player.Name)
end
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
for _, ropeBadge in pairs(workspace.Badges:GetChildren()) do
if ropeBadge:IsA("StringValue") then
coroutine.resume(coroutine.create(
function()
DataStore:SetAsync(player.UserId, player:FindFirstChild(ropeBadge.Value).Value)
end
)
)
end
end
end)
i tried to modify the script but it still didnt work
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("MyDataStore") -- This can be changed to whatever you want
local function saveData(player) -- The functions that saves data
for _, badge in pairs(workspace.Badges:GetChildren()) do
if badge:IsA("StringValue") then
local tableToSave = {
player[badge.Value].Value
}
local success, err = pcall(function()
dataStore:SetAsync(player.UserId, tableToSave) -- Save the data with the player UserId, and the table we wanna save
end)
if success then -- If the data has been saved
print("Data has been saved!")
else -- Else if the save failed
print("Data hasn't been saved!")
warn(err)
end
end
end
end
game.Players.PlayerAdded:Connect(function(player) -- When a player joins the game
local d = 0
for _, badge in pairs(workspace.Badges:GetChildren()) do
if badge:IsA("StringValue") then
local hasRope = Instance.new("BoolValue", player)
hasRope.Name = badge.Value
local data -- We will define the data here so we can use it later, this data is the table we saved
local success, err = pcall(function()
data = dataStore:GetAsync(player.UserId) -- Get the data from the datastore
end)
if success and data then -- If there were no errors and player loaded the data
hasRope.Value = data[d + 1] -- Set the money to the first value of the table (data)
else -- The player didn't load in the data, and probably is a new player
print("The player has no data!") -- The default will be set to 0
end
end
end
end)
game.Players.PlayerRemoving:Connect(function(player) -- When a player leaves
local success, err = pcall(function()
saveData(player) -- Save the data
end)
if success then
print("Data has been saved")
else
print("Data has not been saved!")
end
end)
game:BindToClose(function() -- When the server shuts down
for _, player in pairs(game.Players:GetPlayers()) do -- Loop through all the players
local success, err = pcall(function()
saveData(player) -- Save the data
end)
if success then
print("Data has been saved")
else
print("Data has not been saved!")
end
end
end)
The way I do it is I make a dictionary(index is the value name and the value is the actual value) and save the whole dictionary with set async, then I use getasync to get the dictionary and transfer the values from the dictionary.
Here’s the difference between a dictionary and a table
Create a table by doing local table = {}
Then for each value use table.insert to add it to your datatable. Then you can save the completed datatable using setasync