Hello. I recently made a mirror model but for some reason it is not reflecting some accessories properly. I’m guessing this might be a gimber lock problem but other than that I have no idea why this is happening.
On another note, GUI elements won’t show up unless i put some extra print lines in the module script. Maybe it is a framework issue since it is rendering every frame? I don’t know.
Here is my script:
mirrors (ClientScript)
local Mirrors = workspace.Mirrors
if not Mirrors then error("Mirrors model was not found.") end
local rs = game:GetService("RunService")
local players = game:GetService("Players")
local MirrorModule = require(script.MirrorModule)
local ReflectedObjects = Instance.new("Folder")
ReflectedObjects.Parent = Mirrors
ReflectedObjects.Name = "Reflected Objects"
local mirrorParts = {}
local function GetAndSetMirrors()
for _, inst in Mirrors:GetDescendants() do
if inst:IsA("Part") and string.match(string.lower(inst.Name), "mirror") and not table.find(mirrorParts, inst) then
table.insert(mirrorParts, inst)
local range = inst:FindFirstChild("Range")
if not range then error("The range value for the mirror is not found.") end
local ReflectBoth = inst:FindFirstChild("ReflectOnBothSides")
if not ReflectBoth then error("The ReflectOnBothSides value for the mirror is not found.") end
local bounds = inst:Clone()
bounds.Parent = inst
bounds.Name = inst.Name .. "'s bounds"
bounds.Anchored = true
bounds.CanCollide = false
bounds.Transparency = 1
if ReflectBoth.Value then bounds.Size += Vector3.new((range.Value * 2), 0, 0)
else
bounds.Size += Vector3.new(range.Value, 0, 0)
bounds.CFrame = bounds.CFrame:ToWorldSpace(CFrame.new(range.Value/2, 0, 0))
end
end
end
end
GetAndSetMirrors()
Mirrors.ChildAdded:Connect(function(child)
GetAndSetMirrors()
end)
rs.RenderStepped:Connect(function(dt)
MirrorModule:Clear()
for _, Inst in mirrorParts do
local bounds = Inst:FindFirstChild(Inst.Name .. "'s bounds")
if not bounds then error("The bounds for " .. Inst.Name .. " is not found.") end
MirrorModule:DrawWorld(Inst, ReflectedObjects, bounds)
end
end)
ModuleScript parented to the script above:
local MirrorModule = {}
local Players = game:GetService("Players")
local RenderPacket = {}
local function Gather(array, className)
local gatherings = {}
for x, y in array do
if y:IsA(className) then
table.insert(gatherings, y)
end
end
return gatherings
end
local function GatherIgnoring(array, className, ignoreArray)
local gatherings = {}
for x, y in array do
if y:IsA(className) and not table.find(ignoreArray, y) then
table.insert(gatherings, y)
end
end
return gatherings
end
function MirrorModule:ReplicatePart(mirror: Part, partToReplicate: Instance)
local x, y, z, r00, r01, r02, r10, r11, r12, r20, r21, r22 = mirror.CFrame:ToObjectSpace(partToReplicate.CFrame):GetComponents()
partToReplicate.CFrame = mirror.CFrame:ToWorldSpace(CFrame.new(-x, y, z, r00, -r01, -r02, -r10, r11, r12, -r20, r21, r22))
end
function MirrorModule:DrawCharacter(mirror: Part, char: Model, parentTo: Folder)
char.Archivable = true
local copy = char:Clone()
copy.Parent = parentTo
local hum = copy:FindFirstChildOfClass("Humanoid")
if hum then
hum.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None
end
local scripts = Gather(copy:GetDescendants(), "BaseScript")
local parts = Gather(copy:GetDescendants(), "BasePart")
for _,x in scripts do x:Remove() end
for _, part in parts do
part.Anchored = true
part.CanCollide = false
MirrorModule:ReplicatePart(mirror, part)
end
table.insert(RenderPacket, copy)
end
function MirrorModule:DrawWorld(mirror: Part, parentTo: Folder, bounds: Part?)
local players = {}
local inBoundParts = {}
if bounds~=nil then inBoundParts = workspace:GetPartsInPart(bounds) end
for _, part in GatherIgnoring(workspace:GetDescendants(), "BasePart", {mirror, workspace.Terrain or nil}) do
if bounds~=nil and not table.find(inBoundParts, part) then continue end
if Players:GetPlayerFromCharacter(part.Parent) and not table.find(players, part.Parent) then
MirrorModule:DrawCharacter(mirror, part.Parent, parentTo)
table.insert(players, part.Parent)
elseif not part.Parent:IsA("Accessory") and not Players:GetPlayerFromCharacter(part.Parent) then
local newPart = part:Clone()
newPart.Parent = parentTo
table.insert(RenderPacket, newPart)
MirrorModule:ReplicatePart(mirror, newPart)
end
end
end
function MirrorModule:Clear()
for x, y in RenderPacket do
if y:IsA("Instance") then
y:Remove()
end
end
RenderPacket = {}
end
return MirrorModule
Any help would be appreciated!