I’m trying to save a table of data using the data key (not player). Trying to save it to the game.
I am getting a response in my code telling me that when it gets the data, it isn’t NIL meaning it saves, but it isn’t being placed into the values. How come?
local Datastore = game:GetService("DataStoreService")
local Votes = Datastore:GetDataStore("VotingStorage")
local vote = workspace:FindFirstChild("Vote")
local cooldown = false
local data = {
Grapes = vote.Grapes.Value,
Pasta = vote.Pasta.Value,
Pizza = vote.Pizza.Value,
Salad = vote.Salad.Value
}
local function updateInfo()
local success, err = pcall(function()
local DownloadedData = Votes:GetAsync(Votes)
if DownloadedData and DownloadedData ~= nil then
print(DownloadedData)
print("Roblox data located - Data NOT NIL")
vote.Grapes.Value = DownloadedData.Grapes
vote.Pasta.Value = DownloadedData.Pasta
vote.Pizza.Value = DownloadedData.Pizza
vote.Salad.Value = DownloadedData.Salad
data = {
Grapes = vote.Grapes.Value,
Pasta = vote.Pasta.Value,
Pizza = vote.Pizza.Value,
Salad = vote.Salad.Value
}
end
end)
if success then
return print("Worked")
else
return warn("Error ["..err.."]")
end
end
wait(3)
updateInfo()
print(table.concat(data))
game.ReplicatedStorage.Vote.OnServerEvent:Connect(function(player, option)
for _, vote in pairs(vote:GetChildren()) do
if option == vote.Name then
vote.Value += 1
print(player.Name.." has added a vote to "..vote.Name)
print("Save cooldown status: "..tostring(cooldown))
if cooldown ~= true then
local success, err = pcall(function()
Votes:SetAsync(Votes, data)
end)
if success then
print("Votes successfully saved")
player.PlayerGui.Voting.Frame.Submit.Active = false
player.PlayerGui.Voting.Frame.Submit.Visible = false
cooldown = true
print("Save cooldown status: "..tostring(cooldown))
wait(10)
print("Thanks for voting! Developer debug console: Save success; Cooldown = "..tostring(cooldown).."; Pasta value = "..workspace.Vote.Pasta.Value..";")
wait(50)
cooldown = false
print("Save cooldown status: "..tostring(cooldown))
else
warn("Error ["..err.."]")
end
else
repeat wait() until cooldown == false
local success, err = pcall(function()
Votes:SetAsync(Votes, data)
end)
if success then
print("Votes successfully saved")
cooldown = true
print("Save cooldown status: "..tostring(cooldown))
wait(10)
player:Kick("Thanks for voting! Developer debug console: Save success; Cooldown = "..tostring(cooldown).."; Pasta value = "..workspace.Vote.Pasta.Value..";")
wait(50)
cooldown = false
print("Save cooldown status: "..tostring(cooldown))
else
warn("Error ["..err.."]")
end
end
end
end
end)
The issue here is that it won’t be saved in the order you specified it.
That is because the highest number will be at the top, and the lowest one will be at the bottom of the table in your case, as that’s how ordering works.
I would recommend setting a name for each value in the data table.
Example:
local Data = {
Coins = Coins.Value
}
Coins.Value = Data.Coins
Also: Show me the entire script, it will be easier to understand.
I believe I understand what you’re saying. Here is my entire script:
local Datastore = game:GetService("DataStoreService")
local Votes = Datastore:GetDataStore("VotingStorage")
local vote = workspace:FindFirstChild("Vote")
local cooldown = false
local data = {
Grapes = vote.Grapes.Value,
Pasta = vote.Pasta.Value,
Pizza = vote.Pizza.Value,
Salad = vote.Salad.Value
}
local function updateInfo()
local success, err = pcall(function()
local DownloadedData = Votes:GetAsync(Votes)
if DownloadedData and DownloadedData ~= nil then
print(DownloadedData)
print("Roblox data located - Data NOT NIL")
vote.Grapes.Value = DownloadedData.Grapes
vote.Pasta.Value = DownloadedData.Pasta
vote.Pizza.Value = DownloadedData.Pizza
vote.Salad.Value = DownloadedData.Salad
data = {
Grapes = vote.Grapes.Value,
Pasta = vote.Pasta.Value,
Pizza = vote.Pizza.Value,
Salad = vote.Salad.Value
}
end
end)
if success then
return print("Worked")
else
return warn("Error ["..err.."]")
end
end
wait(3)
updateInfo()
print(table.concat(data))
game.ReplicatedStorage.Vote.OnServerEvent:Connect(function(player, option)
for _, vote in pairs(vote:GetChildren()) do
if option == vote.Name then
vote.Value += 1
print(player.Name.." has added a vote to "..vote.Name)
print("Save cooldown status: "..tostring(cooldown))
if cooldown ~= true then
local success, err = pcall(function()
Votes:SetAsync(Votes, data)
end)
if success then
print("Votes successfully saved")
player.PlayerGui.Voting.Frame.Submit.Active = false
player.PlayerGui.Voting.Frame.Submit.Visible = false
cooldown = true
print("Save cooldown status: "..tostring(cooldown))
wait(10)
print("Thanks for voting! Developer debug console: Save success; Cooldown = "..tostring(cooldown).."; Pasta value = "..workspace.Vote.Pasta.Value..";")
wait(50)
cooldown = false
print("Save cooldown status: "..tostring(cooldown))
else
warn("Error ["..err.."]")
end
else
repeat wait() until cooldown == false
local success, err = pcall(function()
Votes:SetAsync(Votes, data)
end)
if success then
print("Votes successfully saved")
cooldown = true
print("Save cooldown status: "..tostring(cooldown))
wait(10)
player:Kick("Thanks for voting! Developer debug console: Save success; Cooldown = "..tostring(cooldown).."; Pasta value = "..workspace.Vote.Pasta.Value..";")
wait(50)
cooldown = false
print("Save cooldown status: "..tostring(cooldown))
else
warn("Error ["..err.."]")
end
end
end
end
end)
I think it’s the way it saves thats the problem, but I can’t seem to figure out how to fix it.