Unable To Cast Value To Function?

I have a datastore which handles all saving of essential values within the player. After redesigning it after some catastrophic errors, it doesn’t error anymore. The thing is though, it doesn’t save either. That’s because it keeps warning an error within the pcall. Ideas on what is causing it?

local DS = game:GetService("DataStoreService") --- Gets Datastore Service
local CSS = DS:GetDataStore("ChaosSpleefStore_7")
local OwnedDatastore = DS:GetDataStore("OwnedDatastore_8")
local OwnedEffectsDatastore = DS:GetDataStore("OwnedEffectsDatastore")
local ownedPlaceHolder , ownedEffectsPlaceHolder = {}, {}
game.Players.PlayerAdded:Connect(function(player)
	local owned = {}

	local ldbrd = Instance.new("Folder")
	ldbrd.Name = "leaderstats"
	ldbrd.Parent  = player

	local CRW =Instance.new("BoolValue")
	CRW.Parent = player
	CRW.Name = "CRW"
	CRW.Value = false

	local benefits = Instance.new("IntValue")
	benefits.Parent = player
	benefits.Name = "Benefits"
	benefits.Value = 1

	local rank = Instance.new("StringValue")
	rank.Parent = player
	rank.Name = "Rank"
	rank.Value  = "Newbie"

	local spleefCoins = Instance.new("IntValue")
	spleefCoins.Parent = player
	spleefCoins.Name = "SpleefCoins"

	local wins = Instance.new("IntValue")
	wins.Parent = ldbrd
	wins.Name = "Wins"

	local Levels = Instance.new("NumberValue")
	Levels.Parent = player
	Levels.Name = "Level"
	Levels.Value = 1

	local XP = Instance.new("NumberValue", Levels)
	XP.Name = "XP"

	local XP_Limit =Instance.new("NumberValue", Levels)
	XP_Limit.Name = "XP_Limit"
	XP_Limit.Value = 30

	local  Equipped = Instance.new("StringValue")
	Equipped.Parent = player
	Equipped.Name = "Equipped"
	Equipped.Value = "1"

	local  EquippedEffect = Instance.new("StringValue")
	EquippedEffect.Parent = player
	EquippedEffect.Name = "EquippedEffect"
	EquippedEffect.Value = "1"

	local playeruserid = "Player_"..player.UserId
	local data = {}
	local owned
	local success, errormsg = pcall(function()
		data = CSS:GetAsync(playeruserid)
		owned = OwnedDatastore:GetAsync(playeruserid)
		ownedEffectsPlaceHolder = OwnedEffectsDatastore:GetAsync(playeruserid)
	end)
	if success then
		local ListOfValues =  {player.SpleefCoins.Value, player.leaderstats.Wins.Value, player.Benefits.Value,player.Level.Value, player.Level.XP.Value, player.Level.XP_Limit.Value, player.CRW.Value,player.Rank.Value, player.Equipped.Value,player.EquippedEffect.Value}
		for i, v in pairs(ListOfValues) do
			if data ~= nil then
			for indexValue, Value in pairs(data)do
				v = Value
				end
			else
				print("New User Joined")
			end
			
		end

	end



	game.ReplicatedStorage.sendEquippedEffect:FireClient(player, Equipped.Value)
	game.ReplicatedStorage.sendOwnedEffects:FireClient(player, ownedEffectsPlaceHolder)

end)
game.ServerStorage.PassAlongOwnedTrails.Event:Connect(function(o)
	ownedPlaceHolder = o
end)

game.ServerStorage.PassAlongOwnedEffects.Event:Connect(function(owneed)
	ownedEffectsPlaceHolder = owneed
end)




function Save()
	local players = game.Players:GetPlayers()
	for _, player in pairs(players) do
		local userId = "Player_"..player.UserId
		local data = {player.SpleefCoins.Value, player.leaderstats.Wins.Value, player.Benefits.Value,player.Level.Value, player.Level.XP.Value, player.Level.XP_Limit.Value, player.CRW.Value,player.Rank.Value, player.Equipped.Value,player.EquippedEffect.Value}
	
		if data then
			local success, result = pcall(function()
				CSS:UpdateAsync(userId, data)
				if ownedPlaceHolder ~= nil then
					OwnedDatastore:UpdateAsync(userId, ownedPlaceHolder)
				end
				if ownedEffectsPlaceHolder ~= nil then
					ownedEffectsPlaceHolder:UpdateAsync(userId, ownedEffectsPlaceHolder)
				end

			end)
			if not success then
				warn(result)
			else
				return true
			end   
		end
	end


end

while wait(10) do
	if Save()then
		print("Saved Data")
	end
end
game.Players.PlayerRemoving:Connect(function(player)
	Save()
	if Save() then
		print("Saved "..player.Name.."'s data before they left")
	end
end)

game:BindToClose(Save)

1 Like

2nd argument of Datastore:UpdateAsync function must be a function, but you’re passing a table inside Save function.

2 Likes

What function would I add then? The save function? Sorry, I switched from setAsync to updateAsync, due to concerns over data loss. I don’t know it too well

I see you’re setting your values’ parent to the player and not the leaderboard, maybe that is what you’re looking for?

I don’t want some values to be on the leaderboard, which is why I’m doing them under player until I 'm willing to organize them a bit more. The thing is, doing that would mean redeigning my whole system, which I’m not keen on

It’s in the leaderboard folder, not the player

Ah. I see what you mean. I researched a bit and I found out that you can’t save the value directly with update async, instead you use a function like you said.

Looking at the actual focus of update async, I don’t think it will be necessary for some of the things I’m saving. SetAsync would do better as the old values aren’t important. I’ll only use updateAsync for saving tables that store trails etc, as I don’t want to overwrite and potentially lose any player data

1 Like