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)
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)
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)
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)