Help with "Cannot store Instance in data store. Data stores can only accept valid UTF-8 characters" error

I’m trying to make a script to store the player current team when they leave but I keep getting the “Cannot store Instance in data store. Data stores can only accept valid UTF-8 characters.” error but I can’t find what is causing it? here’s the script

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("ObbyDataStore")
game.Players.PlayerAdded:Connect(function(player)
	local data
	
	local success, errorMessage = pcall(function()
		data = DataStore:GetAsync(player.UserId.."-stage")
	end)
	
	if success then
		if data then
			player.Team = game.Teams[data]
		else
			player.Team = game.Teams.Stage1
		end
	else
		player.Team = game.Teams.Stage1
	end
end)


game.Players.PlayerRemoving:Connect(function(player)
	local teamName = player.Team
	
	local success, errorMessage = pcall(function()
		DataStore:SetAsync(player.UserId.."-stage",teamName)
	end)
	
	if success then
		print("players data saved")
	else
		print(errorMessage)
	end
end)

The error says it all, you’re trying to save the Team Instance. I think you want to store its name?

1 Like

I’m just trying to store the team they are in so when they rejoin they are set to that team. I’m also really new to using data stores

This should be the easiest solution.

local teamName = player.Team.Name

Like @Pokemoncraft5290 said, you are saving the Instance, not the name.

2 Likes

Change

local teamName = player.Team

to

local teamName = player.Team.Name
2 Likes

got it thank you! character limit