Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Color3 value not being updated correctly when used in a variable?

I'm currently working on a system whereby a player selects a colour in a GUI, which is then sent to the server to change the Color3 value of a certain group of parts using a RemoteEvent. I'm experiencing a problem whereby, when called in a variable named 'colour', the Color3 value of the parts is set to 0,0,0.

There are no errors in the Output Log.

Here is the associated LocalScript (I have annotated so it is clearer what I am doing and if I've made an error somewhere):

local replicatedStorage = game:GetService("ReplicatedStorage")
local updateColourEvent = replicatedStorage:WaitForChild("UpdateColourEvent")
-- (Above) Setting up RemoteEvent

wait(1)

local hilt = game.Workspace:WaitForChild("Hilt1") -- The model that holds the parts
local forward = script.Parent.Forwards -- The button pressed to trigger the colour change
local back = script.Parent.Backwards -- Not used yet
colour = script.Parent.Colour.BackgroundColor3 -- The current colour the client has selected

colours = { -- Dictionary to hold the available colours
	["Red"] = Color3.new(1,0,0),
	["Green"] = Color3.new(0.4,1,0.5),
	["Blue"] = Color3.new(0.2,0.2,1),
	["Purple"] = Color3.new(0.6,0,1),
	["Cyan"] = Color3.new(0,1,1),
	["Orange"] = Color3.new(1,0.4,0),
	["Pink"] = Color3.new(1,0,0.9)
}



forward.MouseButton1Click:connect(function()
	print("Setting colour...")
	print(colour)
	if colour == colours["Red"] then
		print("Current colour is red, setting to green.")
		script.Parent.Colour.BackgroundColor3 = Color3.new(0.4,1,0.5)
		colour = script.Parent.Colour.BackgroundColor3
	end
	updateColourEvent:FireServer(colour)
end)

-- (Above) Function called when player clicks button to update colour. A test is done to find the current selected colour, and then uses that to switch to the next colour in the list. The selected colour is updated in the variable 'colour', and sent to the Server with the RemoteEvent.

And here is the associated Server Script (also annotated):

--LukeSmasher

local replicatedStorage = game:GetService("ReplicatedStorage")
local updateColourEvent = Instance.new("RemoteEvent", replicatedStorage)
local hilt = script.Parent

updateColourEvent.Name = "UpdateColourEvent"

updateColourEvent.OnServerEvent:connect(function(player,colour)
	print(colour) -- Prints value correctly
	for i, child in pairs(hilt:GetChildren()) do
		if child.Name == "Primary" then -- Parts named 'Primary need to be re-coloured to the value of x
			child.Color = Color3.new(colour) -- Parts' colour given value of 0,0,0
			print(colour) -- 'colour' still has correct value when printed
		end
	end
end)

As described by my annotations, when the players clicks on the button to switch colour, a variable named 'colour' is updated and sent to the Server with the RemoteEvent (in ReplicatedStorage). The Server Script takes the Color3 value in the variable and sets all parts named "Primary" in the model to have that colour.

Instead of setting to the value of 'colour', however, the parts get the Color3 Value, 0,0,0.

Any ideas as to what I've done wrong would be hugely appreciated. I am more than happy to provide more information on my set-up if necessary.

1 answer

✓ Accepted answer

On line 13 of your ServerScript try changing:

	child.Color = Color3.new(colour)

For just:

	child.Color = colour

Since your colour variable is already a Color3 one. Still, not sure if this will fix your problem, but its worth a shot.

Best of lucks!

1 comment

1

That sorted it, thanks a bunch!

This discussion is preserved as it was. New answers, comments, and votes are closed.