Function only coloring one decal

Heres a bit of my code (the one in question)

local ReplicatedStorage = game.ReplicatedStorage
local Event = ReplicatedStorage.NicknameChangeEvent
local running = false

local function rainbow(thing, speed)
	while true do
		if running == true then
			for i = 0, 1, 0.001 * speed do
				if running == false then
					break
				end
				thing.Color3 = Color3.fromHSV(i, 1, 1)
				wait()
			end
		else
			break
		end
	end
end

Event.OnServerEvent:Connect(function(plr, method, input)
	local char = plr.Character
	local descendants = char:GetDescendants()
	
	if method == "Rainbow" then
		if #descendants == 0 then return end
		
		for i = 1, #descendants do
			if descendants[i]:IsA("Decal") or descendants[i]:IsA("Texture") then
				if descendants[i].Name ~= "Bucket" then
					if input == true then
						running = true
						rainbow(descendants[i], 1)
					else
						running = false
					end
				end
			end
		end
    end
end)

It works, it just only applies the rainbow effect to one decal at a time. How would I make it apply to EVERY decal it finds?

2 Likes

Sorry, could you elaborate? I don’t see how this fixes it.

You could then use spawn or coroutine for multi-threading/running multiple functions in parallel (I suggest using coroutine since spawn() seems to have a little bit of delay)

You might be using a global variable ‘running’

local ReplicatedStorage = game.ReplicatedStorage
local Event = ReplicatedStorage.NicknameChangeEvent
local running = false

local function rainbow(thing, speed)
	while true do
		if running == true then
			for i = 0, 1, 0.001 * speed do
				if running == false then
					break
				end
				thing.Color3 = Color3.fromHSV(i, 1, 1)
				wait()
			end
		else
			break
		end
	end
end

local bruh = coroutine.wrap(rainbow)

Event.OnServerEvent:Connect(function(plr, method, input)
	local char = plr.Character
	local descendants = char:GetDescendants()
	
	if method == "Rainbow" then
		if #descendants == 0 then return end
		
		for i = 1, #descendants do
			if descendants[i]:IsA("Decal") or descendants[i]:IsA("Texture") then
				if descendants[i].Name ~= "Bucket" then
					if input == true then
						running = true
						bruh(descendants[i], 1)
					else
						running = false
					end
				end
			end
		end
    end
end)

Like this?
That’s as far as my knowledge of coroutines go lol
Still doesn’t work.

Running it in both these ways should run it I think, I usually used a different method to run coroutines but you can only actually get that answer from testing your code :wink:

Yeah, I did the bottom one. It still only colors one at a time.

Hey there,

There’s a service called CollectionService. You can use it to make couple of stuff to work with a 1 script. Moreover,

	while true do
		if running == true then
			for i = 0, 1, 0.001 * speed do
				if running == false then
					break
				end
				thing.Color3 = Color3.fromHSV(i, 1, 1)
				wait()
			end
		else
			break
		end
	end
end

The while true do here is unnecessary since you have if running == true then..... Instead, you can do while running == true do

And since you have if running == false then… You can change it to if not running then

Try the top one then, coroutine.create() and coroutine.resume()

1 Like

That one also doesn’t work.

(sdsdsdsd)

1 Like

I think for coroutines to run properly, you will have to make a separate function for each coroutine, so try doing this instead

coroutine.resume(coroutine.create(function()
    rainbow(descendants[i], 1)
end))
1 Like

It works.

Now, is there a way to stop the rainbow effect?

I think it’s called coroutine.yield but, I’m not sure how I would use it in this case.

You can just modify your rainbow() function to PAUSE when running = false, so

if running == true then
-- do stuff
end
-- dont do anything if it isnt