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