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

How come my DataStore won't save said values needed to be saved?

---###----------[[DESCRIPTION]]----------###---

I've been working on a game and I just recently tested how the DataStore is going and if it works and it turns out it doesn't save at all and I get no error. What is the problem with my DataStore and why won't it save?

---###----------[[UPDATE]]----------###---

I forgot you could test DataStores in studio so I tested it and I got the error "104: Cannot store Array in data store. Datastores can only accept valid UTF-8 characters.", but I have no idea what it means and I don't know what UTF-8 characters are.

P.S. I haven't scripted in about a year and I never really used tables so if I used them wrong please tell me and explain what I got wrong!

---###----------[[SCRIPT]]----------###---

--###----------[[SERVICES]]----------###--
local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")
local DataStoreService = game:GetService("DataStoreService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--###----------[[DATASTORES]]----------###--
local LeaderBoard1DS = DataStoreService:GetDataStore("LeaderBoard1DS")
--###----------[[LEADERBOARD]]----------###--
Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
--###----------[[BUXSTATS]]----------###--
	local BuxStats = Instance.new("Folder", leaderstats)
	BuxStats.Name = "BuxStats"
--###----------[[OTHERSTATS]]----------###--
	local OtherStats = Instance.new("Folder", leaderstats)
	OtherStats.Name = "OtherStats"
--###----------[[BUX]]----------###--
	local Bux = Instance.new("NumberValue", leaderstats)
	Bux.Name = "Bux"
	Bux.Value = 10000
--###----------[[GAMEPASSES]]----------###--
	local Gems = Instance.new("NumberValue", leaderstats)
	Gems.Name = "Gems"
--###----------[[KILLS]]----------###--
	local Rap = Instance.new("NumberValue", leaderstats)
	Rap.Name = "Rap"
	Rap.Value = 0
--###----------[[ADDBUX]]----------###--
	local AddBux = Instance.new("NumberValue", BuxStats)
	AddBux.Name = "AddBux"
	AddBux.Value = 50
--###----------[[UPGRADECOST]]----------###--
	local BuxCost = Instance.new("NumberValue", BuxStats)
	BuxCost.Name = "BuxCost"
	BuxCost.Value = 5000
--###----------[[TITLE]]----------###--
	local Title = Instance.new("StringValue", OtherStats)
	Title.Name = "Title"
	Title.Value = ""
--###----------[[CASESPEED]]----------###--
	local CaseSpeed = Instance.new("NumberValue", OtherStats)
	CaseSpeed.Name = "CaseSpeed"
	CaseSpeed.Value = 10
--###----------[[CASESPEEDCOST]]----------###--
	local CaseSpeedCost = Instance.new("NumberValue", OtherStats)
	CaseSpeedCost.Name = "CaseSpeedCost"
	CaseSpeedCost.Value = 25000
--###----------[[MULTIPLIER]]----------###--
	local Multiplier = Instance.new("NumberValue", OtherStats)
	Multiplier.Name = "Multiplier"
	Multiplier.Value = 1
end)
--###----------[[LEADERBOARDDS/DATASTORES]]----------###--
Players.PlayerAdded:Connect(function(player)
	local PlayerGui = player:WaitForChild("PlayerGui")
	wait()
	local plrkey = "id_"..player.userId
	local LeaderStats = {
		LeaderStats1 = player.leaderstats.Bux,
		LeaderStats2 = player.leaderstats.Gems,
		BuxStats1 = player.leaderstats.BuxStats.AddBux,
		BuxStats2 = player.leaderstats.BuxStats.BuxCost,
		OtherStats1 = player.leaderstats.OtherStats.CaseSpeed,
		OtherStats2 = player.leaderstats.OtherStats.CaseSpeedCost
	}

	local GetSaved = LeaderBoard1DS:GetAsync(plrkey)
	if GetSaved then
		LeaderStats.LeaderStats1.Value = GetSaved[1]
		LeaderStats.LeaderStats2.Value = GetSaved[2]
		LeaderStats.BuxStats1.Value = GetSaved[3]
		LeaderStats.BuxStats2.Value = GetSaved[4]
		LeaderStats.OtherStats1.Value = GetSaved[5]
		LeaderStats.OtherStats2.Value = GetSaved[6]
	else
		local NumberForSaving = {
			LeaderStats.LeaderStats1.Value,
			LeaderStats.LeaderStats2.Value,
			LeaderStats.BuxStats1.Value,
			LeaderStats.BuxStats2.Value,
			LeaderStats.OtherStats1.Value,
			LeaderStats.OtherStats2.Value
		}
		LeaderBoard1DS:SetAsync(plrkey, NumberForSaving)
	end
end)
Players.PlayerRemoving:Connect(function(player)
	LeaderBoard1DS:SetAsync("id_"..player.userId, {player.leaderstats.Bux.Value, player.leaderstats.Gems.Value, player.leaderstats.BuxStats.AddBux.Value, player.leaderstats.BuxStats.BuxCost.Value, player.leaderstats.OtherStats.CaseSpeed, player.leaderstats.OtherStats.CaseSpeedCost})
end)

1 answer

✓ Accepted answer

You can't save userdata to Datastores. Values such as Vector3 or NumberValue can't be saved to Datastores.

tables can be saved to DataStores, however they have to have valid elements.

10 comments

0

What part is the script trying to save userdata?

0

To me, it looks like these are both userdata: player.leaderstats.OtherStats.CaseSpeed, player.leaderstats.OtherStats.CaseSpeedCost (line 89)

0

They are going to be used for a case system so I'm pretty sure they aren't Userdata.

0

Oh yeah I also changed all the NumberValues to IntValues so that part should be fixed.

0

But I'm still getting the error and I'm pretty sure I can't do anything else.

0

Vector3 and NumberValue were two examples, not an exhaustive list. As for the use of the data, it does not matter. The TYPE of data is the issue, not the purpose of the data.

0

What part of them make you think they are UserData?

0

userdata is any non-lua type. this includes instances (as shown in the example). regardless, you should remove the instances, as they can not be saved

0

OH I forgot to put ".Value"

0

It was a simple typo I'll accept the answer now.

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