Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Model: https://create.roblox.com/store/asset/17284321726/MrMr-Coderkids-Mirror
- These scripts were made for a tutorial, but you may steal it if you wish. ( I don't mind :P )
- These scripts improves on Identity Fraud's and Samson's Mirror (Hotel Story Game).
- These scripts, Instead of reflecting body parts, it reflects *how they move* in comparison to other body parts.
- The result is similar to what would happen if you just inverted the way the bones move(for the limbs)
- The effect is about the most convincing you can get without inverting a mesh*
- I hope you enjoy!
- Please subscribe? ;-;
- - ya boy, @mrmrcoderkid(Youtube)
- *Inverting meshes IS possible, however you cannot enable "DoubleSided" to true at runtime. So it can only be done for preset characters.
- --]]
- -- SERVER SCRIPT --
- local MIRROR_MODULE = require(game.ReplicatedStorage.MMCKmodule)
- local mirrors = workspace.MMCK.Mirrors:GetChildren()
- --[[
- This Script is for the player to reflect characters that are in the server.
- Since the server, under normal circumstances owns NPCs, then
- there is no reason for the Client to handle all of the calculations every frame(.RenderStepped())
- It is better to do the calculations at PhysicsProcess(.HeartBeat()) to save peformance.
- --]]
- game["Run Service"].Heartbeat:Connect(function()
- for i,mirror in mirrors do
- local _npcs = MIRROR_MODULE.FindNonPlayerCharacters(mirror)
- if _npcs ~= nil then
- for i, npc in _npcs do -- npc reflect loop
- local fake_npc
- if not mirror.fakes:FindFirstChild(npc.Name) then
- npc.Archivable = true
- fake_npc = npc:Clone()
- fake_npc.Name = npc.Name
- fake_npc.Humanoid.DisplayDistanceType = "None"
- for i, d in fake_npc:GetChildren() do
- if d:IsA("BaseScript") then
- d:Destroy()
- end
- if d:IsA("BasePart") then
- d.Anchored = true
- d.CanCollide = false
- end
- end
- fake_npc.Parent = mirror.fakes
- local owner_tag = Instance.new("ObjectValue", fake_npc)
- owner_tag.Name = "owner"
- owner_tag.Value = npc
- else
- fake_npc = mirror.fakes:FindFirstChild(npc.Name)
- end
- if npc.Humanoid.Health <= 0 or npc == nil then
- local target = fake_npc or nil
- if target ~= nil then
- target:Destroy()
- end
- else
- MIRROR_MODULE:ReflectCharacter(fake_npc, npc, mirror)
- end
- end
- end
- if #mirror.fakes:GetChildren() > 0 then
- for i, fake in mirror.fakes:GetChildren() do -- remove character clones whose owners no longer belong on workspace.
- if not game.Players:GetPlayerFromCharacter(fake.owner.Value) then
- if not workspace:FindFirstChild(fake.Name) then
- fake:Destroy()
- end
- end
- end
- end
- end
- end)
- -- END OF SERVER SCRIPT --
- -- CLIENT SCRIPT --
- --[[
- This script is for reflecting player characters.
- Since each player has network ownership of their own characters, they can clone themselves.
- This relieves the server from some burdens, saving peformance.
- --]]
- local loaded = false
- -- Proceed only if the character is completely loaded. --
- repeat
- wait()
- until game.Players.LocalPlayer:HasAppearanceLoaded() == true
- loaded = true
- local RunService = game:GetService("RunService")
- local camera = workspace.CurrentCamera
- local mirrors = workspace.MMCK.Mirrors:GetChildren()
- local MIRROR_MODULE = require(game.ReplicatedStorage.MMCKmodule)
- function get_all_player_characters()
- local list_characters = {}
- for i,plr in game:GetService("Players"):GetPlayers() do
- local char = plr.Character or nil
- if char ~= nil then
- table.insert(list_characters, char)
- end
- end
- return list_characters
- end
- RunService.RenderStepped:Connect(function(dt)
- for i, mirror in mirrors do
- local my_char = game.Players.LocalPlayer.Character
- if (my_char.PrimaryPart.Position - mirror.Position).magnitude < MIRROR_MODULE.range and loaded == true then
- if my_char ~= nil then
- local plr_characters = get_all_player_characters()
- if #plr_characters > 0 then
- for i, plrchar in plr_characters do -- npc reflect loop
- local fake_plr_name = tostring(plrchar.Name.."_"..game.Players:GetPlayerFromCharacter(my_char).UserId)
- local fake_npc
- if not mirror.fakes:FindFirstChild(fake_plr_name) then
- plrchar.Archivable = true
- fake_npc = plrchar:Clone()
- fake_npc.Name = fake_plr_name
- fake_npc.Humanoid.DisplayDistanceType = "None"
- for i, d in fake_npc:GetChildren() do
- if d:IsA("BaseScript") then
- d:Destroy()
- end
- if d:IsA("BasePart") then
- d.Anchored = true
- d.CanCollide = false
- end
- end
- fake_npc.Parent = mirror.fakes
- local owner_tag = Instance.new("ObjectValue", fake_npc)
- owner_tag.Name = "owner"
- owner_tag.Value = plrchar
- else
- fake_npc = mirror.fakes:FindFirstChild(fake_plr_name)
- end
- if plrchar.Humanoid.Health <= 0 or plrchar == nil then
- local target = fake_npc or nil
- if target ~= nil then
- target:Destroy()
- end
- else
- MIRROR_MODULE:ReflectCharacter(fake_npc, plrchar, mirror)
- end
- end
- end
- end
- end
- end
- end)
- local player = game.Players.LocalPlayer
- local char = player.Character or script.Parent
- local clone_name = char.Name.."_"..player.UserId
- char.ChildAdded:Connect(function()
- for i,mirror in mirrors do
- if mirror.fakes:FindFirstChild(clone_name) then
- MIRROR_MODULE:ClearCharacter(mirror, mirror.fakes:FindFirstChild(clone_name))
- end
- end
- end)
- char.ChildRemoved:Connect(function()
- for i,mirror in mirrors do
- if mirror.fakes:FindFirstChild(clone_name) then
- MIRROR_MODULE:ClearCharacter(mirror, mirror.fakes:FindFirstChild(clone_name))
- end
- end
- end)
- -- END OF CLIENT SCRIPT --
- -- MODULE SCRIPT --
- --[[
- This is the ModuleScript for the mirror
- All that this is is just a "Library" of functions and variables that
- can be used by any other script that uses require(ModuleScript)
- --]]
- local mirror_module = {}
- mirror_module.range = 90 -- how many studs away for mirror to reflect localplayer
- mirror_module.FindNonPlayerCharacters = function(mirrorPart)
- local range = mirror_module.range
- local npc_list = {}
- for i,v in pairs(workspace:GetChildren()) do
- if v:IsA("Model") and v:FindFirstChildOfClass("Humanoid") and not game.Players:GetPlayerFromCharacter(v) then
- table.insert(npc_list, v)
- end
- end
- return npc_list
- end
- function mirror_module:ClearCharacter(MIRROR, FAKE_CHAR : Model)
- FAKE_CHAR:Destroy()
- end
- function mirror_module:ReflectPart(FAKE_PART : BasePart, REAL_PART, MIRROR)
- local function reflectCFrame(cframe, mirror) -- THX BADGRAPHIX ON DEVFORUM.
- --Get the CFrame relative to the mirror
- local relCF = mirror.CFrame:toObjectSpace(cframe)
- --Get the original CFrame values
- local x, y, z,
- a, b, c,
- d, e, f,
- g, h, i = relCF:components()
- --Reflecting along Z direction - negate Z axis on
- --all vectors
- local newCF = CFrame.new(
- x, y, -z,
- a, b, c,
- d, e, f,
- -g, -h, -i
- )
- --Convert back to world space
- local finalCFrame = mirror.CFrame:toWorldSpace(newCF)
- local x, y, z, r00, r01, r02, r10, r11, r12, r20, r21, r22 = finalCFrame:components()
- finalCFrame = CFrame.new(x, y, z, -r00, r01, r02, -r10, r11, r12, -r20, r21, r22)
- return finalCFrame
- end
- if FAKE_PART ~= nil then
- FAKE_PART.CFrame = reflectCFrame(REAL_PART.CFrame, MIRROR)
- end
- end
- function mirror_module:ReflectCharacter(FAKE_CHAR, REAL_CHAR, MIRROR)
- if REAL_CHAR.Humanoid.RigType == Enum.HumanoidRigType.R6 then
- mirror_module:ReflectPart(FAKE_CHAR["Head"], REAL_CHAR["Head"], MIRROR)
- mirror_module:ReflectPart(FAKE_CHAR["Torso"], REAL_CHAR["Torso"], MIRROR)
- mirror_module:ReflectPart(FAKE_CHAR["Left Arm"], REAL_CHAR["Right Arm"], MIRROR)
- mirror_module:ReflectPart(FAKE_CHAR["Right Arm"], REAL_CHAR["Left Arm"], MIRROR)
- mirror_module:ReflectPart(FAKE_CHAR["Left Leg"], REAL_CHAR["Right Leg"], MIRROR)
- mirror_module:ReflectPart(FAKE_CHAR["Right Leg"], REAL_CHAR["Left Leg"], MIRROR)
- if FAKE_CHAR["Right Arm"]:FindFirstChild("RightGrip") then
- local weldGrip = FAKE_CHAR["Right Arm"]:FindFirstChild("RightGrip")
- weldGrip.Parent = FAKE_CHAR["Left Arm"]
- weldGrip.Part0 = FAKE_CHAR["Left Arm"]
- end
- for i, accessory in REAL_CHAR:GetDescendants() do
- if accessory:IsA("Accessory") then
- local fake_accessory = FAKE_CHAR:FindFirstChild(accessory.Name)
- if fake_accessory.Handle:FindFirstChild("AccessoryWeld") then
- -- for things like shoulder armour plates
- if fake_accessory.Handle.AccessoryWeld.Part1 == FAKE_CHAR["Right Arm"] then
- local fakeWeld = fake_accessory.Handle.AccessoryWeld
- fakeWeld.Part1 = FAKE_CHAR["Left Arm"]
- fakeWeld.C0 *= CFrame.Angles(0,-math.rad(-180),0)
- end
- end
- -- for shoulder pals
- if fake_accessory.Handle ~= nil then
- if fake_accessory.Handle.AccessoryWeld.Part1 == FAKE_CHAR["Torso"] then
- if fake_accessory.Handle:FindFirstChildOfClass("Weld") then
- fake_accessory.Handle:FindFirstChildOfClass("Weld").Enabled = false
- fake_accessory.Handle.Anchored = true
- mirror_module:ReflectPart(fake_accessory.Handle, accessory.Handle, MIRROR)
- end
- end
- end
- end
- end
- end
- if REAL_CHAR.Humanoid.RigType == Enum.HumanoidRigType.R15 then
- mirror_module:ReflectPart(FAKE_CHAR:WaitForChild("Head"), REAL_CHAR:WaitForChild("Head"), MIRROR)
- mirror_module:ReflectPart(FAKE_CHAR:WaitForChild("UpperTorso"), REAL_CHAR:WaitForChild("UpperTorso"), MIRROR)
- mirror_module:ReflectPart(FAKE_CHAR:WaitForChild("LowerTorso"), REAL_CHAR:WaitForChild("LowerTorso"), MIRROR)
- mirror_module:ReflectPart(FAKE_CHAR:WaitForChild("RightUpperArm"), REAL_CHAR:WaitForChild("LeftUpperArm"), MIRROR)
- mirror_module:ReflectPart(FAKE_CHAR:WaitForChild("RightLowerArm"), REAL_CHAR:WaitForChild("LeftLowerArm"), MIRROR)
- mirror_module:ReflectPart(FAKE_CHAR:WaitForChild("RightHand"), REAL_CHAR:WaitForChild("LeftHand"), MIRROR)
- mirror_module:ReflectPart(FAKE_CHAR:WaitForChild("LeftUpperArm"), REAL_CHAR:WaitForChild("RightUpperArm"), MIRROR)
- mirror_module:ReflectPart(FAKE_CHAR:WaitForChild("LeftLowerArm"), REAL_CHAR:WaitForChild("RightLowerArm"), MIRROR)
- mirror_module:ReflectPart(FAKE_CHAR:WaitForChild("LeftHand"), REAL_CHAR:WaitForChild("RightHand"), MIRROR)
- mirror_module:ReflectPart(FAKE_CHAR:WaitForChild("LeftUpperLeg"), REAL_CHAR:WaitForChild("RightUpperLeg"), MIRROR)
- mirror_module:ReflectPart(FAKE_CHAR:WaitForChild("LeftLowerLeg"), REAL_CHAR:WaitForChild("RightLowerLeg"), MIRROR)
- mirror_module:ReflectPart(FAKE_CHAR:WaitForChild("LeftFoot"), REAL_CHAR:WaitForChild("RightFoot"), MIRROR)
- mirror_module:ReflectPart(FAKE_CHAR:WaitForChild("RightUpperLeg"), REAL_CHAR:WaitForChild("LeftUpperLeg"), MIRROR)
- mirror_module:ReflectPart(FAKE_CHAR:WaitForChild("RightLowerLeg"), REAL_CHAR:WaitForChild("LeftLowerLeg"), MIRROR)
- mirror_module:ReflectPart(FAKE_CHAR:WaitForChild("RightFoot"), REAL_CHAR:WaitForChild("LeftFoot"), MIRROR)
- if FAKE_CHAR.RightHand:FindFirstChild("RightGrip") then
- local gripWeld = FAKE_CHAR.RightHand:FindFirstChild("RightGrip")
- gripWeld.Parent = FAKE_CHAR.LeftHand
- gripWeld.Part0 = FAKE_CHAR.LeftHand
- end
- for i, accessory in REAL_CHAR:GetDescendants() do
- if accessory:IsA("Accessory") then
- local fake_accessory = FAKE_CHAR:FindFirstChild(accessory.Name)
- if fake_accessory.Handle:FindFirstChild("AccessoryWeld") then
- -- for things like shoulder armour plates
- if fake_accessory.Handle.AccessoryWeld.Part1 == FAKE_CHAR.RightUpperArm then
- local fakeWeld = fake_accessory.Handle.AccessoryWeld
- fakeWeld.Part1 = FAKE_CHAR.LeftUpperArm
- fakeWeld.C0 *= CFrame.Angles(0,-math.rad(-180),0)
- end
- end
- -- for shoulder pals
- if fake_accessory.Handle ~= nil then
- if fake_accessory.Handle.AccessoryWeld.Part1 == FAKE_CHAR.UpperTorso then
- if fake_accessory.Handle:FindFirstChildOfClass("Weld") then
- fake_accessory.Handle:FindFirstChildOfClass("Weld").Enabled = false
- fake_accessory.Handle.Anchored = true
- mirror_module:ReflectPart(fake_accessory.Handle, accessory.Handle, MIRROR)
- end
- end
- end
- end
- end
- end
- end
- return mirror_module
- -- END OF MODULE SCRIPT --
- -- :3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement