Unable to cast value to Object

So i got this script in serverscriptservice:

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")


game.Players.PlayerAdded:Connect(function(player)
	local character = player.Character or player.CharacterAdded:Wait()
	local shirtId = character:WaitForChild("Shirt").ShirtTemplate
	local pantsId = character:WaitForChild("Pants").PantsTemplate

	local data
	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(player.UserId, {shirtId, pantsId})
	end)
	if success then
		shirtId = data[1]
		pantsId = data[2]
	else
		warn(errormessage)
	end

end)


game.Players.PlayerRemoving:Connect(function(player)
	local character = player.Character or player.CharacterAdded:Wait()
	local shirtId = character:WaitForChild("Shirt").ShirtTemplate
	local pantsId = character:WaitForChild("Pants").PantsTemplate
	local success, errormessage = pcall(function()
		local success, errormessage = pcall(function()
			myDataStore:SetAsync(tostring(player.UserId), {shirtId, pantsId})
		end)
		if success then
			print ("Player Data successfully saved")
		else
			print ("Error saving data")
			warn(errormessage)
		end
	end)
	end)

and it’s suppost to save clothes you bought in-game so after you leave/die you still got the clothes on you, the problem is that whenever i play-test the game the output says “Unable to cast value to Object”

1 Like

GetAsync only needs 1 argument (which is the key), the table containing Ids is unnecessary

1 Like

so i have to just delete the ids, right?

data = myDataStore:GetAsync(player.UserId)
No table needed

i stll get “unable to cast value to object”

In the GetAsync function, remove the second argument; GetAsync only takes one argument (or, extremely uncommonly, a second DataStoreGetOptions argument.)

You’re also using pcalls incorrectly, and your script will break when more than one player joins. Do this instead in the PlayerAdded function:

local success, data = pcall(function()
	return myDataStore:GetAsync(player.UserId)
end)

This sets the data variable to be local in the PlayerAdded context, so it is a separate variable for each player. Also, the pcall in this case does not return an error message.

The same error appears multiple times actually, to solve that, put this at the top of the PlayerAdded function:

local shirtId
local pantsId

Also, in the PlayerRemoving function you shouldn’t be using multiple pcalls. Use only one, like this example in the wiki:

image

I’m pretty sure that will solve your problem but if it isn’t solved after you make these changes I can take another look.

1 Like

i guess it doesn’t work?? like it doesn’t give me any errors/prints/warnings, but it still doesn’t apply my clothes.

What type of Instance is the ShirtTemplate and the PantsTemplate?

Edit: @Mob1leN0OB Assuming that the Instances are a Shirt and a Pants, a DataStore system like the one in this example should be enough to save their IDs:

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local myDataStore = DataStoreService:GetDataStore("myDataStore")

local function onPlayerAdded(player: Player)
	local success, data = pcall(myDataStore.GetAsync, myDataStore, player.UserId)

	if success then
		print(data)
	else
		warn(data)
	end
end

local function onPlayerRemoving(player)
	if player.Character then
		xpcall(myDataStore.SetAsync, warn, myDataStore, player.UserId, {
			player.Character.Shirt.ShirtTemplate,
			player.Character.Pants.PantsTemplate,
		})
	else
		warn(string.format("Unable to save data for Player %i (%s) because Player.Character is nil", player.UserId, player.DisplayName))
	end
end

Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerRemoving)

if RunService:IsStudio() then
	game:BindToClose(function()
		task.wait(1)
	end)
else
	game:BindToClose(function()
		local x, y = 0, 0

		for _, player in Players:GetPlayers() do
			x += 1

			coroutine.wrap(function()
				onPlayerRemoving(player)

				y += 1
			end)()
		end

		repeat task.wait() until y == x
	end)
end

For testing purposes and to show that it works, I made it so that it prints the data retrieved for that player when they join the game

There are some important drawbacks I’d like to address with both my example and the method you’re currently using to manage the data, which are:

  • The character isn’t guaranteed to exist at the moment a player leaves the game, even when joining the game in certain situations
  • In the code you provided you’re attempting to set the IDs for the Shirt and Pants without first checking to see if the character has a Shirt and Pants, which unless you’re changing the character to one that includes them beforehand will cause errors

This is why I personally don’t recommend involving characters directly in your data saving system, since characters are quite unpredictable. Instead I suggest you save a table with boolean values that represent which clothes the player has bought in your game and then giving the player the clothes which are set to true once they join the game, which will prevent many future issues with your data system

it doesn’t work??? like i don’t know if you just can’t save clothes in-game or is the script not working because, it does everything its suppost to do but it just doesn’t apply the shirt and pants thingy

so in the video below i buy the clothes and stuff then leave.

after i join back its the same clothes.

That’s because when you join a game Roblox loads your character wearing the clothes on your profile

You need to find a way to:

  • Tell the data store that the player bought clothes and which clothes they bought
  • Add the clothes to the player once they join the game

I’ll see if I can make an example you can use to do this

i suck at datastores and i actually tried something from my different game, when i buy a shirt a boolvalue inside the player value is set to true, and it will save the boolvalue, and if its true then apply the clothing, but it took too long and it still didnt work.

1 Like

I did research, and saving player’s clothing does happen to be quite challenging to do

This post here on the forum seems to explain it quite thoroughly so hopefully it would help guide you to making your system work:

thats a tooooooooooooooooooon of scripts and words and all of dat, ima try it tommorow and also, thank you for helping.

1 Like

Are you testing the game in Studio or in the Player? If you’ve been only testing it in Studio, follow these instructions to enable DataStores:

(source: Data Stores | Documentation - Roblox Creator Hub)

bro i got it turned on the whole time, if it wasn’t turned on it would say that in the output.