Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Whats wrong with this? i keep getting an error telling me that i can only store UTF-8 characters

i keep getting an error that says "Cannot store Instance in data store. Data stores can only accept valid UTF-8 characters." and i cant figure out why

local dataStoreService = game:GetService("DataStoreService")
local obbyDataStore = dataStoreService:GetDataStore("obbyDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local coins = Instance.new("IntValue",leaderstats)
	coins.Name = "Coins"
	coins.Value = 0
	
	local stage = Instance.new("IntValue",leaderstats)
	stage.Name = "Stage"
	stage.Value = 0
	
	local kills = Instance.new("IntValue",leaderstats)
	kills.Name = "Kills"
	kills.Value = 0
	
	local datastagevar
	
	local success, errormessage = pcall(function()
		datastagevar = obbyDataStore:GetAsync(player.UserId,"-stagevar")
		datastage = obbyDataStore:GetAsync(player.UserId,"-stage")
		datakills = obbyDataStore:GetAsync(player.UserId,"-kills")
		datacoins = obbyDataStore:GetAsync(player.UserId,"-coins")
	end)
	if success then
		if datastagevar then
			player.Team = game.Teams[datastagevar]
		else
			player.Team = game.Teams.Spawn
		end
		coins.Value = datacoins
		kills.Value = datakills
		stage.Value = datastage
	end
end)




game.Players.PlayerRemoving:Connect(function(player)
	local teamName = player.Team
	local success, errormessage = pcall(function()
		obbyDataStore:SetAsync(player.UserId.."-stagevar",teamName)
		obbyDataStore:SetAsync(player.UserId.."-stage",player.leaderstats.Stage.Value)
		obbyDataStore:SetAsync(player.UserId.."-kills",player.leaderstats.Kills.Value)
		obbyDataStore:SetAsync(player.UserId.."-coins",player.leaderstats.Coins.Value)
	end)
	if success then
		print ("Data Saved")
		
	else
		print("Error, i am now sentient human")
		warn(errormessage)
	end
	
	
end)


1 answer

✓ Accepted answer

The problem is that you're trying to store the 'Team' object, and not its name. However, this is a simple fix:

local dataStoreService = game:GetService("DataStoreService")
local obbyDataStore = dataStoreService:GetDataStore("obbyDataStore")

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local coins = Instance.new("IntValue",leaderstats)
    coins.Name = "Coins"
    coins.Value = 0

    local stage = Instance.new("IntValue",leaderstats)
    stage.Name = "Stage"
    stage.Value = 0

    local kills = Instance.new("IntValue",leaderstats)
    kills.Name = "Kills"
    kills.Value = 0

    local datastagevar

    local success, errormessage = pcall(function()
        datastagevar = obbyDataStore:GetAsync(player.UserId,"-stagevar")
        datastage = obbyDataStore:GetAsync(player.UserId,"-stage")
        datakills = obbyDataStore:GetAsync(player.UserId,"-kills")
        datacoins = obbyDataStore:GetAsync(player.UserId,"-coins")
    end)
    if success then
        if datastagevar then
            player.Team = game.Teams[datastagevar]
        else
            player.Team = game.Teams.Spawn
        end
        coins.Value = datacoins
        kills.Value = datakills
        stage.Value = datastage
    end
end)




game.Players.PlayerRemoving:Connect(function(player)
    local teamName = player.Team.Name -- Changed code here
    local success, errormessage = pcall(function()
        obbyDataStore:SetAsync(player.UserId.."-stagevar",teamName)
        obbyDataStore:SetAsync(player.UserId.."-stage",player.leaderstats.Stage.Value)
        obbyDataStore:SetAsync(player.UserId.."-kills",player.leaderstats.Kills.Value)
        obbyDataStore:SetAsync(player.UserId.."-coins",player.leaderstats.Coins.Value)
    end)
    if success then
        print ("Data Saved")

    else
        print("Error, i am now sentient human")
        warn(errormessage)
    end


end)


Have a good day!

This discussion is preserved as it was. New answers, comments, and votes are closed.