What am I trying to accomplish is that a model will detect if it is in the same position as another object, and if it is, I make the parts in the model red. Whenever it is not, it turns back to its normal colours.
I am well aware that I can have a part that surrounds the object(Make it a default colour in one state and red when it is in the ‘colliding’ state) to illustrate the same effect however that is not what I want.
I don’t know how to execute this, I just have a theory that I could potentially save the colours into a table before it changes colour and then I ‘revert’ back manually. Is there some more effective ways to do this, a function I may not know yet? Anything helps, thank you for reading thus far.
So your talking about making the actual model, with probably more than one default color, red, and then being able to revert it to its original state when not colliding with another object?
I have seen some placement systems that make use of viewport frames. You can manipulate things like the ImageColor3, and ambient lighting within the frame. I think it would also probably be more efficient than moving a group of parts around at once.
I don’t know a whole lot about them. I’m sure it could simplify this problem a bit, but it might take some solid work to do it.
Referring to the question of changing colors of a model, I find a decent way is to save the colors in a table then revert back to them when you want to change the color.
local objColors = {}
for i,v in pairs(model:children()) do
if v:IsA("BasePart") then
objColors[v] = v.BrickColor
end
end
-- later..
for part,color in pairs(objColors) do
part.BrickColor = color
end
As for the detecting collision of another object, if each object has some flat base, you can take advantage of 2D collision to determine if there is an object colliding with another. This is how I made mine.
You have to make your own 2D collision detection btw. I can send you mine if I can find it.
I don’t want to come off as rude but that is literally the opposite of what I said I didn’t want. That sets a specific colour onto a part, it doesn’t ‘revert’ to a certain colour. If a model has multiple colours that script wouldn’t work. Also using Touch wouldn’t be efficient for my purposes.
It would be a lot better way because detecting the same position as your object has to other object can be difficult and is very small chance to actually have the same position to it. That’s why I think that would be the best way. Also if you’d like to revert color then you should create a script that has it’s own color located as local and once player TouchEnded it fire client and revert it back to normal.
Best of luck
I already have a script that detects the object in the same position as another xdd… I don’t really want to instance another script just to hold onto the colours of an object. But thanks for attempting to help me out though. Much appreciated <3
You are right, I definitely need to improve on that aspect, people has said that about me many times so I don’t take it as you meant any ill intent. I will strive to improve
I can’t find my new one, but here’s one I used to use:
function checkCollision(Obj,Tab)
for i,v in pairs(Tab) do
local w1,h1,w2,h2 = Obj.AbsoluteSize.X,Obj.AbsoluteSize.Y,v.AbsoluteSize.X,v.AbsoluteSize.Y
local Center1,Center2 = Obj.AbsolutePosition+Vector2.new(w1/2,h1/2),v.AbsolutePosition+Vector2.new(w2/2,h2/2)
if (Center1.X+w1/2 > Center2.X-w2/2 and Center1.X-w1/2 < Center2.X+w2/2)
and (Center1.Y+h1/2 > Center2.Y-h2/2 and Center1.Y-h1/2 < Center2.Y+h2/2) then
return true,i,v
end
end
return false
end
I think this one has the issue where if one object is completely inside the other, it might cause issues, but I can’t remember.
Also, it’s meant for actual GUI 2D, so you’d have to edit it to work with 3D models.
I wouldn’t recommend using this one, but instead figure out exactly how it works and making sure your version doesn’t have the issue I mentioned above.
If you’re absolutely certain that there will never be more than one way that a model can become “colored”, then the “storing the colors of the model in a table” works well. Here’s an example implementation of that:
local partColors = {}
function colorModel( model, color )
for _, d in pairs(model:GetDescendants()) do
if d:IsA("BasePart") then
partColors[d] = d.Color
d.Color = color
end
end
end
function uncolorModel( model )
for _, d in pairs(model:GetDescendants()) do
if d:IsA("BasePart") then
d.Color = partColors[d]
end
end
end
There are cases where this won’t work though. If you apply two color effects after each other, without removing the first color effect, then the part colors get overwritten. You can fix that by only storing the part colors when color is applied to an uncolored part, like so:
local partColors = {}
function colorModel( model, color )
for _, d in pairs(model:GetDescendants()) do
if d:IsA("BasePart") then
if not partColors[d] then
partColors[d] = d.Color
end
d.Color = color
end
end
end
function uncolorModel( model )
for _, d in pairs(model:GetDescendants()) do
if d:IsA("BasePart") then
d.Color = partColors[d]
partColors[d] = nil
end
end
end
But what if you want to change the “original” color while a color effect is working? E.g. a blinking lamp that changes between black and yellow each second, but still needs to be colored red (and not change to yellow or black) when being placed in an invalid spot. Or a model that becomes more and more black the more damaged it becomes? What you’d need is some way for different “layers” of color effects on a model. This is a bit more effort, so don’t bother with it unless you really want to.
As for collision detection, you might want to look up EgoMoose’s RotatedRegion3. You can use that to do collision detection between spheres and parts, and I think a few other shapes.