Unable to cast Array?

I’m trying to make a dataStore that holds 2 values.

But whenever i set the add 2 values to the sync, It says Unable to cast Array, But when i only Store 1 Value, It saves succesfully

local DataStore = game:GetService("DataStoreService")
local theStore = DataStore:GetDataStore("ILikeCheese")

-- When player joins
game.Players.PlayerAdded:Connect(function(player)
	-- Loading Data from player
	local success, data = pcall(function()
		return theStore:GetAsync("Player_"..player.UserId)
	end)

	if success then
		-- Place values in player once the datastore reading worked
		print("our data is alive")

		local VelueHammer = Instance.new("IntValue", player)
		VelueHammer.Name = "ValueHammer"

		if data then
			print("Found you")
			VelueHammer.Value = data
		else
			print("Can't find this dude, This dude probably played the game for the first time")
			VelueHammer.Value = 0
		end

		-- Connecting this only if datastore try was succesful
		local clickDetector = game.Workspace.Spawn.Spawn["Meshes/stick"].ClickDetector
		clickDetector.MouseClick:Connect(function()
			VelueHammer.Value = VelueHammer.Value + 1
			local playerUserId = "Player_"..player.UserId
			print(playerUserId)
		end)
	else
		-- datastore reading failed, retry with a loop or handle the error, do not let player to play without a proper datastore reading
		print("The data literally died, roblox issues, retry on a loop")
	end
end)

-- When player leaves
game.Players.PlayerRemoving:Connect(function(player)
	
	local data = {
		VelueHammer = player:FindFirstChild("ValueHammer").Value,
		ValueWood = player:FindFirstChild("ValueWood").Value
		}
	
	
	
	
	
	
	
	
	-- Save datastore only if the player has the data
	if player:FindFirstChild("ValueHammer") then
		theStore:SetAsync("Player_"..player.UserId, data.VelueHammer)
	end
end)

Where do you add “2 values to the sync”? Which line errors?

1 Like

You changed “data.VelueHammer” to only “data” on the SetAsync line?

1 Like

With these small changes, now it loads the 2 values and saves both values when player leaves:

local DataStore = game:GetService("DataStoreService")
local theStore = DataStore:GetDataStore("SomeDataStore_01") -- your datastore name

-- When player joins
game.Players.PlayerAdded:Connect(function(player)
	-- Loading Data from player
	local success, data = pcall(function()
		return theStore:GetAsync("Player_"..player.UserId)
	end)

	if success then
		-- Place values in player once the datastore reading worked
		print("our data is alive")
		
		local ValueHammer = Instance.new("IntValue", player)
		ValueHammer.Name = "ValueHammer"
		local ValueWood = Instance.new("IntValue", player)
		ValueWood.Name = "ValueWood"
		
		if data then
			print("player in datastore found")
			if data["ValueHammer"] then
				ValueHammer.Value = data["ValueHammer"]
			else
				ValueHammer.Value = 0
			end
			if data["ValueWood"] then
				ValueWood.Value = data["ValueWood"]	
			else
				ValueWood.Value = 0
			end
			
		else
			print("no player in datastore, should be new player")
			ValueHammer.Value = 0
			ValueWood.Value = 0
		end
		
		-- Connecting this only if datastore try was succesful
		
		local clickDetector = game.Workspace.Spawn.Spawn["Meshes/stick"].ClickDetector
		clickDetector.MouseClick:Connect(function()
			ValueHammer.Value = ValueHammer.Value + 1
			local clickDetector = game.Workspace.ClickaPArt.ClickDetector
			local playerUserId = "Player_"..player.UserId
			print(playerUserId)
		end)
		
	else
		-- datastore reading failed, retry with a loop or handle the error, do not let player to play without a proper datastore reading
		print("The data literally died, roblox issues, retry on a loop")
	end
end)

-- When player leaves
game.Players.PlayerRemoving:Connect(function(player)
	-- Save datastore only if the player has the data
	if player:FindFirstChild("ValueHammer") then
		local data = {ValueHammer = player:FindFirstChild("ValueHammer").Value,
			ValueWood = player:FindFirstChild("ValueWood").Value}
		
		local succ, err = pcall(function()
			return theStore:SetAsync("Player_"..player.UserId, data) 
		end)

		if succ then
			warn(data, "saved")	
		else
			warn("Unable to save the data, do retries on a loop")
			warn(err)
		end
		
	end
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.