Part and light not changing color

This function should change the cube and light color randomly every half second but when I call it, nothing happens.

--in a module
function customFuncs.TestCube(model:Model)
	local cube : Part = model.Part;
	local light : PointLight = cube.PointLight
	
	while wait(0.5) do
		local color = BrickColor.random();
		
		cube.BrickColor = color;
		light.Color = cube.Color;
	end
end

I’m calling the function with this function

--also in a module
function module:PlaceItem(itemId, player:Player, modelCFrame)
	local model = invRef.gameItems[itemId].ItemModel;
	if not model then warn("id:"..itemId.." missing model"); return; end
	
	local clone = model:Clone();
	
	clone:SetAttribute("Owner", player.UserId);
	
	clone:PivotTo(modelCFrame);
	
	clone.Parent = workspace;
	
	invRef.gameItems[itemId].Function(model, itemId); --called here
end

That function is being called here in a server script

placementModule:PlaceItem(
	0,
	game:GetService("Players"):WaitForChild("Jakethegreat_321"),
	CFrame.new(0, 10, 0)
);

You can probably ignore most everything in these code blocks but I showed them just in case

I know that the cube and light exist, because print shows them and no errors or anything show up, but for some reason they don’t change color.

It’s probably a simple typo or something embarrassing, but I can’t find it lol

1 Like

Is BrickColor.random() a thing?
Try:

	while wait(0.5) do
		local color = BrickColor.random();
		print(BrickColor)
		cube.BrickColor = color;
		light.Color = cube.Color;
	end

[/quote]

It’s in the docs BrickColor | Documentation - Roblox Creator Hub

brickcolor has a property called .Color returns color3 formatted so you can use that for the light I guess instead of putting a brickcolor which is not admited assuming the light uses color3

if nothing happens u can put a print statement in the function

printing the colors of the cube and light and it’s right… but they still aren’t actually changing color
Screenshot 2023-10-19 020210

function customFuncs.TestCube(model:Model)
	local cube : Part = model.Part;
	local light : PointLight = cube.PointLight;
	
	while wait(0.5) do
		local color = BrickColor.random().Color;
		
		cube.Color = color;
		light.Color = cube.Color;
		
		print("cube:", cube.Color, "light:", light.Color)
	end
end

it has to be a roblox bug or something because this is really weird

well, when i tried the code u sended me it worked perfectly, the only issue here is that this cube and model dont actually exist

as this man says, is the module executed by server?
there might be a replication issue going on.

It exists on both the client and server, it’s run by the server, nothing happens on either side.

you can check if the “cube” variable exists or not u can print it’s name to see if it returns “nil” or not

I did that before as I said in the OP, and if it wasn’t successfully passed to the function then it would error when I try to assign Color

would i show u a video of how i run the function?

sure, but i don’t think it’ll work the same as mine because you don’t have all the structure code I have in place

well i ran out of ideas for you… i also dont know why this is happening, does it just like print out, but never actually changes the color? if so thats pretty weird

yup. the weirdest part is that i made almost the exact same thing a few hours ago work, so if it’s a roblox bug it showed up recently

well the only possible reason for this is that you arent calling this function properly, as the instance/model ur looking for isnt correct, check for any misspelling

edit: if you’ve done all correct, the only possible reason i could think of would to be restart studio, if that doesnt work uninstall and install it back

try this?

function customFuncs.TestCube(model: Model)
	local cube: Part = model.Part
	local light: PointLight = cube.PointLight
	
	task.spawn(function()
		while task.wait(0.5) do -- task.wait > wait
			local color = BrickColor.Random().Color -- for some reason, roblox likes PascalCasing more than camelCasing, also yes you can do BrickColor.new().Color
		
			cube.Color = color -- fun fact: semicolon on every line isn't needed
			light.Color = color
		end
	end) -- using task.spawn allows other code to run after you call this function
end
1 Like

I’m sorry everyone, I was right. It is embarrassing. I was passing the model in replicated storage to the function instead of the clone… Thanks for trying lol

1 Like

I realized what the probably is but I didn’t know about task.spawn, that’s a pretty useful function

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