Hi! So, I always used Ray.new when scripting anything to do with raycasting, so that’s what I was familiar with. After reading Intro to Raycasting, I decided to give this method a shot.
I’m having difficulty getting the correct intended behavior. I am attempting to make it so when a new ClosestInteractionPart is found, it casts a ray to check if there is any parts between the character and the InteractionPart. The purpose of this is so players can’t use the buttons behind walls.
My Code
function GetClosestInteractionPart()
local Buttons = GetInteractionParts()
local ClosestInteractionPart = nil
local ClosestDistance = math.huge
for i,v in pairs(Buttons) do
local Distance = Vars.Player:DistanceFromCharacter(v.Position)
if Distance <= MaxDistance and Distance < ClosestDistance then
ClosestInteractionPart = v
ClosestDistance = Distance
end
end
if ClosestInteractionPart then
local Params = RaycastParams.new()
Params.FilterDescendantsInstances = {Vars.Player.Character}
Params.FilterType = Enum.RaycastFilterType.Blacklist
local RaycastResult = workspace:Raycast(Vars.Player.Character.Head.Position, ClosestInteractionPart.Position, Params)
EndAttachment.Position = ClosestInteractionPart.Position
if RaycastResult then
EndAttachment.Position = RaycastResult.Position
local Hit = RaycastResult.Instance
Hit.BrickColor = BrickColor.new('Black')
ClosestInteractionPart = nil
end
end
return ClosestInteractionPart or false
end
I am honestly clueless to make it work how I intend it to. I would appreciate any assistance.
