What do you want to achieve? When player touches Colorable part I want to change the color fo the Colorable, I made this on client to look that it is quick, now I need to do it on Server to add the Coins, etc.
What is the issue? I made similar post 2 hours back, and now I modified the system a bit and it doesn’t work.
What solutions have you tried so far?
player.CharacterAdded:Connect(function(character)
for _,Part in pairs(character:GetChildren()) do
if Part:IsA("BasePart") then
Part.Touched:Connect(function(hit)
print(hit)
if hit.Name == "Colorable" then
local c = hit
if c.Color ~= player.PlayerColorValue.Value and player.Paint.Value ~= 0 then
c.Color = player.PlayerColorValue.Value
print("Coloring")
end
end
end)
end
end
end)
end)
It never prints Coloring, when I do print(hit) it prints SpawnLocation (Where people spawn)
But even when I walk on the Colorables it doesn’t print Colorables…
Every suggestion is appreciated!
EDIT: I want to do it when a part of a player touches the Colorable, e.g. Leg or Arm or Foot, because of optimization.
I would suggest switching sides of the .Touched event. In my opinion that would exhaust the server since the player will be in a constant moving state. What you should do instead is create a table of these Colorable parts (Basically create a file and insert all of the color parts there) and cast .Touched events for them rather than for the character itself.
Like so:
local ColorParts = game.Workspace.ColorParts:GetChildren() -- Your folder of these color parts?
for Index, Object in pairs(ColorParts:GetChildren()) do
if Object.ClassName == "Part" or Object.ClassName == "MeshPart" then
Object.Touched:Connect(function(HitPart)
if HitPart.Parent and HitPart.Parent:FindFirstChild("Humanoid") and game:GetService("Players"):GetPlayerFromCharacter(HitPart.Parent) then
---Your function
end
end)
end
end
It’s going to be a lot better to keep the .Touched on the character. It’s the same both ways, you need to fire when the leg touches the part or the part touches the leg.
If you start connecting to the .Touched on all of those parts you will just use up a lot of the ram to store those connections that will rarely be used. It’s a lot more resource efficient to only have like 5 * player connections than it is to have 2000 connections.
At the potential cost of expense though. Touch events on character parts are more expensive than of bricks themselves. If you put a touch event on the leg, it’s going to fire every time it touches the ground or something else. That is going to be more destructive than many connections with a negligible cost.
Not using the touched event is also a viable option. Downcasting every frame to check if there’s a new colour being stood on is something that would work provided the colours are on the ground.
Reference:
This one is for all limbs though. Principle still applies.
Yeah. If a character jumps into a wall the event will fire thousands of times in like 2 seconds. Compared to 10 being fired by all of the players on just the parts. So definitely go with the parts if you are still going to use .Touched.