Why My Data Script Not Working?

Hello Devs My Data Script Not Working ! :frowning:
My Script In ServerScriptService
My Script Code :

--// Script Locals
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("ServerPlayerData")
local RunService = game:GetService("RunService")
--// Main Script
game.Players.PlayerAdded:Connect(function(player)
	--// Money
	local Money = Instance.new("IntValue")
	Money.Name = "Money"
	Money.Value = 0
	Money.Parent = player
	--// Cars Inventory
	local CarsInventory = Instance.new("Folder")
	CarsInventory.Name = "CarsInventory"
	CarsInventory.Parent = player
	--// skin Inventory
	local skinInventory = Instance.new("Folder")
	skinInventory.Name = "SkinInventory"
	skinInventory.Parent = player
	--// Equipped Car
	local EquippedCar = Instance.new("StringValue")
	EquippedCar.Name = "EquippedCar"
	EquippedCar.Parent = player
	--// Equipped Color
	local EquippedColor = Instance.new("Color3Value")
	EquippedColor.Name = "EquippedColor"
	EquippedColor.Parent = player
	--// Equipped Spoiler
	local EquippedSpoiler = Instance.new("StringValue")
	EquippedSpoiler.Name = "EquippedSpoiler"
	EquippedSpoiler.Parent = player
	--// Data Values Creator
	local data
	local success, errorMsg = pcall(function()
		data = DataStore:GetAsync(player.UserId)
	end)
	if data ~= nil then
		if data.EquippedCar then
			EquippedCar.Value = data.EquippedCar
		end
		if data.EquippedColor then
			EquippedColor.Value = data.EquippedColor 
		end
		if data.Money then
			Money.Value = data.Money
		end
		if data.EquippedSpoiler then
			EquippedSpoiler.Value = data.EquippedSpoiler
		end
		if data.CarsInventory then
			for i, v in pairs(data.CarsInventory) do
				local val = Instance.new("StringValue")
				val.Name = v
				val.Parent = CarsInventory
			end
		end
		if data.Traps then
			for i, v in pairs(data.Traps) do
				local val = Instance.new("StringValue")
				val.Name = v
				val.Parent = CarsInventory
			end
		end
	end
end)
game.Players.PlayerRemoving:Connect(function(player)
	local data = {};
	data.Money = player.Money.Value;
	data.EquippedCar = player.EquippedCar.Value;
	data.EquippedColor = player.EquippedColor.Value;
	data.EquippedSpoiler = player.EquippedSpoiler.Value;
	data.CarsInventory = {};
	data.Traps = {};
	for i, v in pairs(player.SkinInventory:GetChildren()) do
		table.insert(data.CarsInventory,v.Name);
	end;
	for i, v in pairs(player.CarsInventory:GetChildren()) do
		table.insert(data.Traps,v.Name);
	end;
	local success, errorMsg = pcall(function()
		DataStore:SetAsync(player.UserId, data);
	end);
	if success then
		print("Successfully Saved")
	elseif errorMsg and not success then
		warn(player.Name.."`s Data Save Error , Error Message : "..errorMsg)
	end;
end);
1 Like

Did the Script print anything from here

2 Likes

Have you enabled Studio Access to API Services?

1 Like

Yes My Api Service Turned On
The Error Not For This

You could change this if-statment to this?

if success then
print("Data saved!")
else
warn(errormsg)
end

Hey! I’ve tried out this script and it returned this "KasimAkr`s Data Save Error , Error Message : 104: Cannot store Dictionary in data store. Data stores can only accept valid UTF-8 characters. ".

You can’t store Color3Values on there own.
While a Color3Value cannot be stored in a datastore, a table containing R, G, and B values may be stored and subsequently converted back into a Color3 value, we call this serializing.

Theres a bunch of resources on saving color3values. I partically like this one.

(Feel free to mark this as a solution!)

oh wait I forgot something.

At this piece of code here.

add a new variable btw

local savedData

local success, errmsg = pcall(function()
Saveddata = DataStore:SetAsync(player.UserId, data)
end)

if success then
    data = savedData
else
warn("DataStores failed!")
end

Just make sure to make another variable called savedData and do the same thing with GetAsync() as well. And make sure to set the data’s value to the savedData variable whenever you make a pcall() using the datastoreservice.If you still don’t understand, then you can go look for a tutorial on youtube. I hope this helps! :slightly_smiling_face: