Unable to cast Value to Object

This is really getting on my nerves, I created this game over 2 years ago and back then all my scripts worked perfectly fine. Now that I come back to the game to try and make updates, I’m getting this error on multiple scripts that haven’t been changed. I’ll give one example because if someone can help me figure it out, I can figure the rest out myself.

Error:

  • Unable to cast value to Object - Server - Ammo:10
  • Stack Begin - Studio
  • Script ‘ServerScriptService.Player.Ammo’, Line 10 - Studio - Ammo:10

Here’s the code:

local ds = game:GetService("DataStoreService")

game.Players.PlayerAdded:Connect(function(plr)
	local ammo = Instance.new("Folder", plr)
	ammo.Name = "Ammo"
	
	local pas = ds:GetDataStore("PistolAmmoSave")
	local pa = Instance.new("IntValue", ammo)
	pa.Name = "Pistol"
	pa.Value = pas:GetAsync(plr.UserId, pa.Value) or 20
	pa.Changed:Connect(function()
		pas:SetAsync(plr.UserId, pa.Value)
	end)
end)

You should probably use a table to store your data and then getting the data.

How? I’ve never used tables for datastores in the years that I’ve been scripting and never had this issue until now

So I’ve taken a bit of older code I found on one of my places and I changed it a bit with your code. Only thing you should be mindful about that this code doesn’t really work on Studio, make sure to test on Live Game.

local ds = game:GetService("DataStoreService")
local pas = ds:GetDataStore("PistolAmmoSave")

local sessionData = {}

function PlayerAdded(plr)
	local ammo = Instance.new("Folder", plr)
	ammo.Name = "Ammo"

	local pa = Instance.new("IntValue", ammo)
	pa.Name = "Pistol"
	pa.Parent = ammo
	
	local success = nil
	local playerData = nil
	local attempt = 1

	repeat
		success, playerData = pcall(function()
			return pas:GetAsync(plr.UserId)
		end)
		
		attempt += 1
		if not success then
			warn(playerData)
			task.wait(3)
		end
	until success or attempt == 5
	
	if success then
		print("Connected to the data store")
		if not playerData then
			playerData = {
				["pa"] = 20
			}
		end
		sessionData[plr.UserId] = playerData
	else
		warn("Unable to get data for ".. plr.Name)
		plr:Kick("Preventing data loss") -- this is optional
	end
	
	pa.Value = sessionData[plr.UserId].pa
	pa.Changed:Connect(function()
		sessionData[plr.UserId].pa = pa.Value
	end)
	
	ammo.Parent = plr
end

function PlayerRemoving(plr)
	if sessionData[plr.UserId] then
		local success = nil
		local errorMsg = nil
		local attempt = 1
		
		repeat
			success, errorMsg = pcall(function()
				pas:SetAsync(plr.UserId, sessionData[plr.UserId])
			end)

			attempt += 1
			if not success then
				warn(errorMsg)
				task.wait(3)
			end
			
		until success or attempt == 5
		
		if success then
			print("Data saved for ".. plr.Name)
		else
			warn("Unable to save for ".. plr.Name)
		end
	end
end

function ShutDown()
	for _, player in ipairs(game.Players:GetPlayers()) do
		task.spawn(function()
			PlayerRemoving(player)
		end)
	end
end

game.Players.PlayerAdded:Connect(PlayerAdded)
game.Players.PlayerRemoving:Connect(PlayerRemoving)
game:BindToClose(ShutDown)