Error while trying to load table values from DS

I really have no idea why isn’t it working, if someone has any ideas I would be grateful!

local DSS = game:GetService("DataStoreService")
local colorDS = DSS:GetDataStore("SaveCarColor")

local paint = script.Parent.Parent:WaitForChild("Body"):WaitForChild("Paint")
local part1 = paint:WaitForChild("Paint")

local spawned = false

local function loadColor(key)
	
	print("loading...")
	
	local Data = colorDS:GetAsync(key)
	
	if Data then
		
		for i, part in pairs(paint:GetChildren()) do

			if part:IsA("MeshPart") then

				part.Color.R = Data[1]
				part.Color.G = Data[2]
				part.Color.B = Data[3]

			end

		end
		
		warn("car colors, loaded successfully!")
		
	else
		
		warn("no data")
		
	end
	


end

local function saveWhenColorChange()
	
	local ownerval = script.Parent.Parent:WaitForChild("Owner")
	
	local plr = game:GetService("Players"):FindFirstChild(ownerval.Value)

	if plr then

		local key = plr.UserId.."-COLORCARSAVE"

		script.Parent.Value = part1.Color

		colorDS:SetAsync(key, {script.Parent.Value.R, script.Parent.Value.G, script.Parent.Value.B})
		print("saved")

	end
	
end


script.Parent.Parent:GetPropertyChangedSignal("Parent"):Connect(function()
	
	wait(1)
	
	local ownerval = script.Parent.Parent:WaitForChild("Owner")
	
	local plr = game:GetService("Players"):FindFirstChild(ownerval.Value)
	
	local key = plr.UserId.."-COLORCARSAVE"
		
		if script.Parent.Parent.Parent == game:GetService("Workspace") then

			spawned = true
			warn("spawned!")

			loadColor(key)

		end
	
end)

part1:GetPropertyChangedSignal("Color"):Connect(saveWhenColorChange)

The error it prints out:

The error is self-explanatory, as it just means that Color3.R is a getter function, which means it only returns the Red value of a Color3 object, and cannot assign it. If you want to assign color the here’s a way to do it.

local color = Color3.fromRGB(Data[1], Data[2], Data[3])
part.Color = color
1 Like

So, the color is loading but I got another issue.
Everytime it loads, it just loads the color black for the part, no matter what color it “saved”

I’m sorry that is my mistake. The values that you save to the table are not in the range of 0 to 255, but Color3.fromRGB takes values from 0 to 255. Try replacing the code i provided with this.

local color = Color3.new(Data[1], Data[2], Data[3])
part.Color = color
1 Like

Works perfectly! Thank you so much for the help!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.