How to revert part back to original properties?

Hello DevForums! I got a script which, when a part obscures a ray between the camera and the player, it changes the part color to be really blue.

Code:

local function color()
	local Part1 = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")
	local Part2 = game.Workspace.CurrentCamera
	local RayOrigin = Part1.Position
	local RayDirection = (Part2.CFrame.Position - Part1.Position).Unit*100

	local RaycastParameters = RaycastParams.new()
	RaycastParameters.FilterDescendantsInstances = {game.Players.LocalPlayer.Character}
	RaycastParameters.FilterType = Enum.RaycastFilterType.Blacklist
	local RaycastResult = workspace:Raycast(RayOrigin, RayDirection, RaycastParameters)

	if RaycastResult then
		local hitPart = RaycastResult.Instance
		hitPart.BrickColor = BrickColor.new("Really blue")
	end
end

while wait(0.2) do
	color()
end

While it works, once the block stops obscuring the camera, it does not revert back to its original color, which got me to wonder about how I can revert a part’s color back to its original color once it is no longer in the way of the camera.

You could store the properties beforehand in a table then apply them again after you finished.

1 Like

How would I be able to apply them again once they are no long in the way of the ray between the camera and the player?

Here’s how you would go about this:

local yourpart = script.Parent -- just an example
local material = yourpart.Material
local color = yourpart.BrickColor()
--now you can revert back to these by simply callin them
local function color()
	local Part1 = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")
	local Part2 = game.Workspace.CurrentCamera
	local RayOrigin = Part1.Position
	local RayDirection = (Part2.CFrame.Position - Part1.Position).Unit*100

	local RaycastParameters = RaycastParams.new()
	RaycastParameters.FilterDescendantsInstances = {game.Players.LocalPlayer.Character}
	RaycastParameters.FilterType = Enum.RaycastFilterType.Blacklist
	local RaycastResult = workspace:Raycast(RayOrigin, RayDirection, RaycastParameters)
    local color = hitpart.BrickColor() -- now you can refrence this when you need it. You know how to change color, right?

	if RaycastResult then
		local hitPart = RaycastResult.Instance
		hitPart.BrickColor = BrickColor.new("Really blue")
	end
end

while wait(0.2) do
	color()
end

Sorry to continue troubling you, but would you put this script:

local yourpart = script.Parent -- just an example
local material = yourpart.Material
local color = yourpart.BrickColor()
--now you can revert back to these by simply callin them

Before this?

local function color()
	local Part1 = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")
	local Part2 = game.Workspace.CurrentCamera
	local RayOrigin = Part1.Position
	local RayDirection = (Part2.CFrame.Position - Part1.Position).Unit*100

	local RaycastParameters = RaycastParams.new()
	RaycastParameters.FilterDescendantsInstances = {game.Players.LocalPlayer.Character}
	RaycastParameters.FilterType = Enum.RaycastFilterType.Blacklist
	local RaycastResult = workspace:Raycast(RayOrigin, RayDirection, RaycastParameters)
    local color = hitpart.BrickColor() -- now you can refrence this when you need it. You know how to change color, right?

	if RaycastResult then
		local hitPart = RaycastResult.Instance
		hitPart.BrickColor = BrickColor.new("Really blue")
	end
end

while wait(0.2) do
	color()
end

Or do you do something else?

Once you find the part, you would need to save it by doing what I showed above.

Sorry again, but I still don’t get it. Do you put the first script in the “if RaycastResult then”?