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.
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.
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!
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?
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?
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.
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.
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.
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
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
-- 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