Need help with Datastore not saving

So. I’m working on this datastore script for my roblox game, and I’m using a table with 3 different values. The odd part is the fact that only 2 of the values are saving (level and cash) but the 3rd one won’t (banned). I have no idea what the problem is as I don’t do much work with datastore.

local datastore = game:GetService("DataStoreService")
local playerdata = datastore:GetDataStore("PlayerData")

local function create_table(player)
	local player_stats = {}
	
	for _, stat in pairs(player.stats:GetChildren()) do
		player_stats[stat.Name] = stat.Value
	end
	return player_stats
end

local function onplayerexit(player)
	local player_stats = create_table(player)
	local sucess, err = pcall(function()
		local playerUserId = "player_"..player.UserId
		playerdata:SetAsync(playerUserId, player_stats)
	end)
	
	if not sucess then
		warn("could not save")
	end
end

local function onplayeradded(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "stats"
	leaderstats.Parent = player
	
	local Level = Instance.new("IntValue")
	Level.Name = "Level"
	Level.Parent = player.stats

	local Cash = Instance.new("IntValue")
	Cash.Name = "Cash"
	Cash.Parent = player.stats
	
	local banned = Instance.new("BoolValue")
	banned.Name = "banned"
	banned.Parent = player.stats
	

	local playerUserId = "player_"..player.UserId
	local data = playerdata:GetAsync(playerUserId)

	if data then
		Cash.Value = data["Cash"]
	leaderstats.Level.Value = data["Level"]

	else
		leaderstats.Cash.Value = 0
		leaderstats.Level.Value = 0
	end
end

game.Players.PlayerAdded:Connect(onplayeradded)
game.Players.PlayerRemoving:Connect(onplayerexit)

Any help is much appreciated.

I believe in datastore:setasync, it doesn’t get the name of it, just the value.
So in here

Cash.Value = data["Cash"]
	leaderstats.Level.Value = data["Level"]

Replace [“Cash”] to [1] then replace [“Level”] to [2]

Instead of using if not use this:

if err then
warn("could not save")
end

for some reason if not never works…

1 Like

I just realized, I didn’t have the value I added saved to datastore. Sorry for wasting your time.

1 Like