How to set color of a model

i have something really simple all i need to do is set color of this model to red/green depending on whether it can be placed
here is my script

local PhysicsService = game:GetService("PhysicsService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")

local events = ReplicatedStorage:WaitForChild("Events")
local towers = ReplicatedStorage:WaitForChild("Towers")
local spawnTowerEvent = events:WaitForChild("SpawnTower")
local camera = workspace.CurrentCamera
local gui = script.Parent

local towerToSpawn = nil
local canPlace = false

local function MouseRaycast(blacklist)
	local mousePosition = UserInputService:GetMouseLocation()
	local mouseRay = camera:ViewportPointToRay(mousePosition.X, mousePosition.Y)
	local raycastParams = RaycastParams.new()

	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	raycastParams.FilterDescendantsInstances = blacklist

	local raycastResult = workspace:Raycast(mouseRay.Origin, mouseRay.Direction * 1000, raycastParams)


	return raycastResult
end

local function RemovrPlaceholderTower()
	if towerToSpawn then
		towerToSpawn:Destroy()
	end
end

local function AddPlaceholderTower(name)

	local towerExists = towers:FindFirstChild(name)
	if towerExists then
		towerToSpawn = towerExists:Clone()
		towerToSpawn.Parent = workspace.Towers

		for i, object in ipairs(towerToSpawn:GetDescendants()) do
			if object:IsA("BasePart") then
				PhysicsService:SetPartCollisionGroup(object, "Towers")
			end
		end
	end
end




gui["main-game"].Frame.Frame.Spawn.Activated:Connect(function()
	AddPlaceholderTower("Pistol")
end)

UserInputService.InputBegan:Connect(function(input, processed)
	if processed then
		return
	end

	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		if canPlace then
			spawnTowerEvent:FireServer(towerToSpawn.Name, towerToSpawn.PrimaryPart.CFrame)
			RemovrPlaceholderTower()
		end
	end
end)


RunService.RenderStepped:Connect(function()
	if towerToSpawn then
		local child = towerToSpawn:GetChildren()
		local result =  MouseRaycast({towerToSpawn})
		if result and result.Instance then
			if result.Instance.Parent.Name == "TowerArea" then
				canPlace = true
				child.BrickColor = Color3.new(1,0,0)
			else
				canPlace = false
			end
			local x = result.Position.X
			local y = result.Position.Y + towerToSpawn.Humanoid.HipHeight + 1.95 + (towerToSpawn.PrimaryPart.Size.Y / 2)
			local z = result.Position.Z

			local cFrame = CFrame.new(x,y,z)
			towerToSpawn:SetPrimaryPartCFrame(cFrame)
		end
	end
end)

3 Likes

Try

for i, v in pairs(Child) do
	v.Color = Color3.new(1,0,0)
end
1 Like

this works, however it tries to set color for stuff like anim saves, any idea how i exclude items from get descendants

1 Like
function CheckProperty(Item,Prop)
	local Found, err = pcall(function()
		local P = Item[Prop]
	end)
	if Found then
		return true
	else
		return false
	end
end
for i, v in pairs(Child) do
	if CheckProperty(v,"Color") then
		v.Color = Color3.new(1,0,0)
	end
end
2 Likes

Set the names of the items you don’t want to be colored in a table, when doing the for loop check if the name is not in the table before changing the color

OR : check the class name if you don’t want to have a giant table of names

1 Like
function Paint(model : Model, color : Color3)
	for _, v in pairs(model:GetChildren()) do
		if v:IsA("BasePart") then
			v.Color = color
		end

		if v:IsA("SpawnLocation") then
			v.TeamColor = BrickColor.new(color)
		end
	end
end

would i call this function, with a color 3 color, in the spot where i am checking whether it can be placed???