Client Coloring

  1. What do you want to achieve? Okay, so I got Colorable Parts around the game, and I want to do this;
    Every time some MeshPart of player’s character touches the colorable, if it is Arm, Leg or so. I want to change the color of the Colorable, the color of the Colorable will be PlayerColorValue inside the player.

  2. What is the issue? I want this on client, I will do it on the server too, where I will add the stats and stuff, reason I want it on Client is that player will feel that the Coloring is quick. I had it on Server until now, but it was laggy. So I thought about switching it like this.

  3. What solutions have you tried so far? I looked at Developer Hub, I also tried this;

local player = game.Players.LocalPlayer
function Color(player)
	repeat wait() until player.Character
	local character = player.Character
	for _,p in next, character:GetChildren() do
		if p:IsA("MeshPart") then
			print("Touched")
		end
	end
end

do Color(player) end

I tried doing this so far, but it doesn’t even print the “Touched”

If anyone could help me with this situation I would be very happy! :slight_smile:

To be clear, you want to change the player’s color of whatever touched that part?

When player touches part, I want to change color of that part.

EDIT: There is lots of Colorables, so this would be better for Optimization.

Lets say the player’s right foot touches that part, you want the right foot to become the part’s color?
Or do you want it the other way around? The right foot touches the part and the part’s color becomes the right foots color?

No, Right Foot touches part, and the Part the foot touched will change color. The new color will be value stored in a Player.

I can do that Color Change, I just don’t know about that Touch.

Oh that is simple, you could literally do this without values. Once the part is touched, if its parent contains a Humanoid then you do script.Parent.Color = hit.Color, I could write up an example if you want?

1 Like

Sure, but I want to do it when the Foot is touched.

I can work with .Touched Events normally.

for _, Part in pairs(workspace.Colorables:GetChildren()) do
    Part.Touched:Connect(function(hit)
        if hit.Parent == Player.Character then
            hit.Color = PlayerColorValue.Value
        end
    end)
end
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
  if hit.Color ~= script.Parent.Color then
    script.Parent.Color = hit.Color
  end
end
end)

Pretty simple code, I added an if statement too, to also make sure that the part color doesn’t keep changing if they are the same. :wink:

That’s what I had earlier, but it was too laggy, since there are more than 2500 Colorables.

That’s not what I meant, I need it like this:

The player is walking and making colors, and when some MeshPart of Character touches Colorable then change the color of the Colorable.

I had it like this earlier, but yeah. It was laggy.

Add a wait(.5) statement.

for _, Part in pairs(workspace.Colorables:GetChildren()) do
	wait(.5)
    Part.Touched:Connect(function(hit)
        if hit.Parent == Player.Character then
            hit.Color = PlayerColorValue.Value
        end
    end)
end

That’ll make it run every .5 seconds, instead of instantly, this should prevent lag.

Again, this is when Colorable is Touched. Is there any way making when Leg is Touched, for example?

Try this!

for _, Part in pairs(workspace.Colorables:GetChildren()) do
	wait(.5)
    Part.Touched:Connect(function(hit)
	if hit.Name == "LeftFoot" or hit.Name == "RightFoot" then --for example
        if hit.Parent == Player.Character then
            hit.Color = PlayerColorValue.Value
		end
        end
    end)
end

I know what you mean, I can do this too.
But I’m asking if there is any way making it when any MeshPart from Character is Touched.
I had it with Colorables earlier, I can still do it. But this would be better for optimization.

Use this thread, https://developer.roblox.com/en-us/api-reference/property/Instance/ClassName
Using ClassName will make it work with Meshparts, you could do if Instance.ClassName == “MeshPart” then ect.

In example;

for _, Part in pairs(workspace.Colorables:GetChildren()) do
	wait(.5)
    Part.Touched:Connect(function(hit)
	if hit.ClassName == "Meshpart" and hit.Name == "LeftFoot" or hit.Name == "RightFoot" then --for example, you could remove LeftFoot and RightFoot if you want :)
        if hit.Parent == Player.Character then
            hit.Color = PlayerColorValue.Value
		end
        end
    end)
end

The thing which was causing lags this way were those thousands of TouchInterests.

So add a wait() statement, this should eliminate lag…

for _, Part in pairs(Player.Character:GetChildren()) do
    If Part:IsA("BasePart") then
       Part.Touched:Connect(function(hit)
           if hit.Name == "Colorable" then
               hit.Color = PlayerColorValue.Value
           end
       end)
   end
end
1 Like

Try this:

-- typing this in devforum

local debouncearray = {} -- setup a debounce array
local function ChangeColor(part)  -- setup a function that changes colors
for i = 1, #debouncearray do -- loop through the db array
if debouncearray[i] == part.Name then return end -- checks if the part name is in the db array
end --closes loop

local ColorArray = workspace.Colorables:GetChildren() -- get an array of colorables


local dbindex = #debouncearray + 1 -- get the debounce index
debouncearray[dbindex] = part.Name -- add the part name to the db array

local RNG = Random.new() -- setup a random number generator
local index = RNG:NextInteger(1, #ColorArray) -- get a random number
part.Color = ColorArray[index] -- set the part's color to the random color
wait() -- wait

debouncearray[dbindex] = nil -- remove it from the debounce
end

-- assuming you have the character defined
for i, v in pairs(character:GetChildren()) do  -- loop through the char
if v:IsA("Part") then -- check if the instance is a part
v.Touched:Connect(ChangeColor) -- connect the changed event to the function
end -- close the if statement
end -- close the loop