Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

DataStore with table not saving?

I did this DataStore Script but it's not working could someone please help me? im trying to do "table.insert()" and I dont know why not working

local DataService = game:GetService("DataStoreService")
local DS = DataService:GetDataStore("DATASTORE")

game.Players.PlayerAdded:Connect(function(player)
	
	local Folder = Instance.new("Folder",player)
	Folder.Name = "leaderstats"
	
	local Cash1 = Instance.new("IntValue",Folder)
	Cash1.Name = "Gold"
	
	local Lv1 = Instance.new("IntValue", Folder)
	Lv1.Name = "Level"
	
	local Stats = DS:GetAsync(player.UserId)
	
	if Stats == not nil then
		Cash1.Value = Stats[1]
		Lv1.Value = Stats[2]
	else
		Cash1.Value = 0
		Lv1.Value = 1
	end
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local Save = {}
	
	table.insert(Save, player.leaderstats.Gold.Value)
	table.insert(Save, player.leaderstats.Level.Value)
	
	DS:SetAsync(player.UserId, Save)
	
end)

1 answer

✓ Accepted answer

Here's your code, this also helps it so that if the script loads longer than usual, all players will get their data loaded.

This should also fix your saving issue, and yes, you are using table.insert in a correct manner.

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local DataStorage = DataStoreService:GetDataStore("DATASTORE")

local InitiateDataStorage = function(Player)
	if not Player or not Player:IsA("Player") then
		return
	end
	local LeaderStatistics = Instance.new("Folder",Player)
	LeaderStatistics.Name = ("leaderstats");
	local Cash = Instance.new("IntValue",LeaderStatistics)
	Cash.Name = ("Gold");
	local Level = Instance.new("IntValue",LeaderStatistics)
	Level.Name = ("Level");
	local Statistics
	pcall(function()
		Statistics = DataStorage:GetAsync(Player.UserId)
	end)
	if not Statistics then
		Cash.Value = 0
		Level.Value = 0
		print("Saved data is not available, or it doesn't exist yet.")
	else
		Cash.Value = Statistics[1]
		Level.Value = Statistics[2]
	end
end

for _,Player in pairs(Players:GetPlayers()) do
	InitiateDataStorage(Player)
end
Players.PlayerAdded:Connect(InitiateDataStorage)

local SaveData = function(Player)
	if not Player or not Player:IsA("Player") then 
		return
	end
	local Save = {};
	local Success,Returned = pcall(function()
		table.insert(Save,Player.leaderstats.Gold.Value)
		table.insert(Save,Player.leaderstats.Level.Value)
		DataStorage:SetAsync(Player.UserId,Save)
	end)
	if Success then
		print("Save successful!");
	end
end

Players.PlayerRemoving:Connect(SaveData)

2 comments

0

I've used some methods such as pcall, you may want to investigate that global function some more for this type of stuff because it's pretty important for this case.

0

thx!! it worked. Thx so much!

This discussion is preserved as it was. New answers, comments, and votes are closed.