Change color gui

Hello @everyone! I would like to ask for a script to change the color of the car body. When you get into the driver’s seat, you have a button on the screen, when you click on it, you can change the color of the car (enter RGB numbers). If it’s not difficult for you and you can help, then thank you in advance. Have a good day!

(GUI already done)

Try this in the GUI [LocalScript]:

local redValue = script.Parent.RedValue -- Change this to the name of the red TextBox!
local blueValue = script.Parent.BlueValue -- Change this to the name of the blue TextBox!
local greenValue = script.Parent.GreenValue -- Change this to the name of the green TextBox!
local car

for i, v in pairs(workspace:GetDescendants()) do
if v:IsA("Model") and v:FindFirstChild("VehicleSeat") then -- Gets all cars.
v.VehicleSeat.Changed:Connect(function()
if v.VehicleSeat.Occupant == game.Players.LocalPlayer then -- Checks if the player is the driver of the car.
-- Sets the car variable to that car and opens the gui.
car = v
script.Parent.Visible = true
car.VehicleSeat.Changed:Connect(function()
if occupant == nil then -- Checks if we left the car.
-- Resets the car variable and removes the gui.
car = nil
script.Parent.Visible = false
end
end)
end
end)
end
end

local function ChangeColor()
-- Actually changes the color.
for i, part in pairs (car.Body:GetChildren()) do -- Make a seperate model/folder for the body of the car!
part.Color = Color3.FromRGB(redValue.Text, blueValue.Text, greenValue.Text)
end
end

-- There are two methods to change the color. Choose which one you want!
-- Method 1
redValue.FocusLost:Connect(changeColor)
blueValue.FocusLost:Connect(changeColor)
greenValue.FocusLost:Connect(changeColor)

-- Method 2
local button = script.Parent.ChangeButton

button.Activated:Connect(changeColor)

While writing the code I assumed that:

  1. you use a-chassis
  2. you want a button which when u click it, a frame pops up which has a TextBox for red, green and blue and then there is a button which when pressed, changes the color of the car to the RGB values that you put in the text boxes

And I’m not sure how ur GUI is set up so you will probably have to change some variables in the LocalScript

Also, in order for it to work, all the parts that should be painted should be called something specific like Color or Paint or something like that, and then that name should be put in the server script’s CarBodyPartName variable

But anyway, here’s how you can setup the code:

Create a RemoteEvent inside ReplicatedStorage and rename it to ChangeCarBodyColorRE

Then put a LocalScript into ur frame:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ChangeCarBodyColorRE = ReplicatedStorage.ChangeCarBodyColorRE

local UI = script.Parent

local Car = UI.Parent.Car.Value

local OpenButton = UI.OpenButton -- In this variable put the button that u need to press in order to open the frame which has the RGB text boxes
local ColorFrame = UI.ColorFrame -- In this variable put the frame that has the RGB text boxes

local R = ColorFrame.R -- In this variable put the R textbox
local G = ColorFrame.G -- In this variable put the G textbox
local B = ColorFrame.B -- In this variable put the B textbox

local ChangeButton = ColorFrame.ChangeButton -- In this variable put the button that u need to press to finally change the color of the car to the RGB values in the text boxes

OpenButton.Activated:Connect(function()
	ColorFrame.Visible = not ColorFrame.Visible
end)

ChangeButton.Activated:Connect(function()
	local R = tonumber(R.Text)
	local G = tonumber(G.Text)
	local B = tonumber(B.Text)
	
	local Color = Color3.fromRGB(R, G, B)
	
	ChangeCarBodyColorRE:FireServer(Car, Color)
	
	ColorFrame.Visible = false
end)

And finally, put a Script into ServerScriptService:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ChangeCarBodyColorRE = ReplicatedStorage.ChangeCarBodyColorRE

local CarBodyPartName = "Color"

ChangeCarBodyColorRE.OnServerEvent:Connect(function(Player, Car, Color)
	local Character = Player.Character
	if Character then
		local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
		if Humanoid then
			if typeof(Car) == "Instance" and Car:IsDescendantOf(workspace) and typeof(Color) == "Color3" then
				local DriveSeat = Car:FindFirstChild("DriveSeat")
				if DriveSeat and (DriveSeat:IsA("VehicleSeat") or DriveSeat:IsA("Seat")) and DriveSeat.Occupant == Humanoid then
					for _, v in pairs(Car:GetDescendants()) do
						if v:IsA("BasePart") and v.Name == CarBodyPartName then
							v.Color = Color
						end
					end
				end
			end
		end
	end
end)
1 Like

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