Getting this error even though I have a color

I keep getting this error when I am in the 4th index and subtract it by 1
“ServerScriptService.Script:6: invalid argument #3 (Color3 expected, got nil)”
code:

local ChangeSkinColorEvent = game.ReplicatedStorage.ChangeSkinColor

local skinColors = {

Color3.fromRGB(29, 18, 7),

Color3.fromRGB(106, 61, 21),

Color3.fromRGB(176, 127, 86),

Color3.fromRGB(245, 199, 135)

}

local index = math.random(1,#skinColors)

ChangeSkinColorEvent:FireServer(skinColors[index])

script.Parent.TextLabel.Text = tostring(table.find(skinColors,skinColors[index]))

script.Parent.RightArrow.MouseButton1Click:Connect(function()

index += 1

ChangeSkinColorEvent:FireServer(skinColors[index])

script.Parent.TextLabel.Text = tostring(table.find(skinColors,skinColors[index]))

if index == #skinColors then

index = 0

end

end)

script.Parent.LeftArrow.MouseButton1Click:Connect(function()

index -= 1

ChangeSkinColorEvent:FireServer(skinColors[index])

script.Parent.TextLabel.Text = tostring(table.find(skinColors,skinColors[index]))

if index == 1 then

index = #skinColors + 1

end

end)

Use print(index) in various spots to see if the index is correct.

1 Like

This looks to be a client side script send the server script to so we can see the error because the error message you got seems to from the server.

1 Like
local ChangeSkinColorEvent = game.ReplicatedStorage.ChangeSkinColor

ChangeSkinColorEvent.OnServerEvent:Connect(function(player, skinColor)
	for _,v in pairs(player.Character:GetChildren()) do
		if v:IsA("BasePart") then
			v.Color = skinColor
		end
	end
end)

Ok, I Got It!

local ChangeSkinColorEvent = game.ReplicatedStorage.ChangeSkinColor

local skinColors = {

	Color3.fromRGB(29, 18, 7),

	Color3.fromRGB(106, 61, 21),

	Color3.fromRGB(176, 127, 86),

	Color3.fromRGB(245, 199, 135)

}

local index = math.random(1,#skinColors)

ChangeSkinColorEvent:FireServer(skinColors[index])

script.Parent.TextLabel.Text = tostring(table.find(skinColors,skinColors[index]))



script.Parent.RightArrow.MouseButton1Click:Connect(function()
	if index == #skinColors then
		index = 0
	end
	index += 1
	ChangeSkinColorEvent:FireServer(skinColors[index])
	script.Parent.TextLabel.Text = tostring(table.find(skinColors,skinColors[index]))


end)

script.Parent.LeftArrow.MouseButton1Click:Connect(function()	
	if index == 1 then
		index = #skinColors
	else
		index -= 1
	end
	ChangeSkinColorEvent:FireServer(skinColors[index])

	script.Parent.TextLabel.Text = tostring(table.find(skinColors,skinColors[index]))

end)

Let me know if it works!

Here a short video:

i Think that is this what you want.

1 Like

Use Math.Clamp to keep your index from going below 1 or higher than the table length (which is 4).

adding:

index = math.clamp(index + 1, 1, #skinColors)

subtracting:

index = math.clamp(index - 1, 1, #skinColors)
1 Like

This seems to be happening cause you set the index to 0 when it reached 4 but arrays in lua start from 1 causing it to be nil, also there is a change math.random breaks your script by returning 1 or #skinColors. Here is a working version of your code:

local ChangeSkinColorEvent = game.ReplicatedStorage.ChangeSkinColor

local skinColors = {
	Color3.fromRGB(29, 18, 7),
	Color3.fromRGB(106, 61, 21),
	Color3.fromRGB(176, 127, 86),
	Color3.fromRGB(245, 199, 135)
}

local index = math.random(1, #skinColors)

ChangeSkinColorEvent:FireServer(skinColors[index])
script.Parent.TextLabel.Text = index 

script.Parent.RightArrow.MouseButton1Click:Connect(function()
	index += 1
	--the checks are done above the :FireEvent() in case math.random returns 1 or #skinColors breaking the script
	if index > #skinColors then 
		--arrays in lua start from 1!
		index = 1
	end
	ChangeSkinColorEvent:FireServer(skinColors[index])
	script.Parent.TextLabel.Text = index
end)

script.Parent.LeftArrow.MouseButton1Click:Connect(function()
	index -= 1
	if index < 1 then 
		index = #skinColors
	end
	ChangeSkinColorEvent:FireServer(skinColors[index])
	script.Parent.TextLabel.Text = index
end)