Hello!
I am currently making a fake body script for my game and ran into a issue regarding tool equipping.
To be clear, my script uses 2 separate models.
- A character model (consists of a torso and legs).
- And arms
When I equip a tool, my fake body disappears for a split second then reapppears.
I have not been able to figure out where in my script that is causing a problem with my script
Gif:
I apologize if the script is messy, I never took the time to organize it and make it look cleaner
Code:
local Camera = workspace.CurrentCamera
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local RunService = game:GetService("RunService")
local DistanceFromRealBody = script:WaitForChild("Distance")
local PlayerModel, ViewmodelPlayerModel = nil, nil
local cf = CFrame.new
local angles = CFrame.Angles
-- // Character
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid") or nil
local Torso = Character:WaitForChild("Torso") or nil
local Left_Shoulder = Torso:WaitForChild("Left Shoulder") or nil
local Right_Shoulder = Torso:WaitForChild("Right Shoulder") or nil
local RightArm = Character:WaitForChild("Right Arm" or nil)
local LeftArm = Character:WaitForChild("Left Arm")
-- // Locals
local Camera = workspace.CurrentCamera
-- // Settings
local offset = Vector3.new(0,-1.8,-1)
local use_left_arm_welds = false
local updateSpeed = .4
-- // Player
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
local Description = nil
local success, fail = pcall(function()
return Players:GetHumanoidDescriptionFromUserId(Player.UserId)
end)
if success then
Description = Players:GetHumanoidDescriptionFromUserId(Player.UserId)
else
Description = Players:GetHumanoidDescriptionFromUserId(20936474)
end
local ignored_limbs = {
["Head"] = true;
["Left Arm"] = true;
["Right Arm"] = true;
["HumanoidRootPart"] = true
}
local transparency_limbs = {
}
local allowed_accessoryTypes = {
Enum.AccessoryType.Back;
Enum.AccessoryType.Neck;
Enum.AccessoryType.Front;
Enum.AccessoryType.Waist;
}
-- // Tool
local rightgrip
local grip
local welded = false
local offsetValue = Instance.new("Vector3Value", script)
offsetValue.Name = "Offset"
offsetValue.Value = offset
function isInFirstPerson(bool)
return (Camera.Focus.p - Camera.CFrame.p).Magnitude < 0.6 and ViewmodelPlayerModel and PlayerModel
end
function weldTool(tool)
task.spawn(function()
if not tool or not tool:FindFirstChild("Handle") then return end
if not isInFirstPerson() then return end
rightgrip = RightArm:WaitForChild("RightGrip", .1)
if rightgrip and ViewmodelPlayerModel:WaitForChild("Right Arm", .1) and RightArm then
rightgrip.Part0 = ViewmodelPlayerModel:WaitForChild("Right Arm")
welded = true
tool.Unequipped:Connect(function()
unweldTool()
end)
else
return
end
end)
end
function unweldTool()
task.spawn(function()
if rightgrip and RightArm then
rightgrip.Part0 = RightArm
end
welded = false
end)
end
function updatePos(rig)
if rig then
local pos = (Camera.CFrame * cf(offsetValue.Value))
rig.CFrame = rig.CFrame:Lerp(pos, 1)
end
end
for _,limb in pairs(Character:GetChildren()) do
if limb:IsA("Part") then
local success, failure = pcall(function()
return Humanoid:GetLimb(limb)
end)
if success then
if Humanoid:GetLimb(limb) then
if not table.find(ignored_limbs, limb.Name) then
limb.LocalTransparencyModifier = 0
table.insert(transparency_limbs, limb)
-- // Accessory
for _, accessory in pairs(Character:GetChildren()) do
if accessory:IsA("Accessory") then
if accessory.AccessoryType then
if table.find(allowed_accessoryTypes, accessory.AccessoryType) then
table.insert(transparency_limbs, accessory:WaitForChild("Handle"))
end
end
end
end
end
end
end
end
end
function camLook(LookVector)
local LookVector = LookVector.Unit
local UpVector = Vector3.new(0, 1, 0)
return LookVector:Dot(UpVector)
end
local AccessoryType_BLACKLIST = {1,2,3}
local cameraOffsets = false
Player.CharacterAdded:Connect(function(character)
Character = character
cameraOffsets = false
end)
RunService.RenderStepped:Connect(function()
-- Viewmodel
if isInFirstPerson() then
if Character:FindFirstChildWhichIsA("Tool") and not welded then
task.wait(0.05)
weldTool(Character:FindFirstChildWhichIsA("Tool"))
end
else
unweldTool()
end
-- Viewmodel
local function lerpPlayer(pos)
if PlayerModel then
if pos then
if PlayerModel.PrimaryPart then
local PrimaryPart = PlayerModel.PrimaryPart
PrimaryPart.CFrame = PrimaryPart.CFrame:Lerp(pos, 1)
end
end
end
end
local function lerpPlayerV(pos)
if ViewmodelPlayerModel then
if pos then
if ViewmodelPlayerModel.PrimaryPart then
local PrimaryPart = ViewmodelPlayerModel.PrimaryPart
PrimaryPart.CFrame = PrimaryPart.CFrame:Lerp(pos, 1)
end
end
end
end
local function addDescription()
if PlayerModel and ViewmodelPlayerModel then
local getDescription = Players:GetHumanoidDescriptionFromUserId(Player.UserId)
local humanoid = PlayerModel:WaitForChild("Humanoid")
humanoid:ApplyDescription(getDescription)
local humanoid2 = ViewmodelPlayerModel:WaitForChild("Humanoid")
humanoid2:ApplyDescription(getDescription)
for i,v in pairs(PlayerModel:GetChildren()) do
if v:IsA("Accessory") then
if table.find(AccessoryType_BLACKLIST, v.AccessoryType.Value) then
v:Destroy()
end
end
end
for i,v in pairs(ViewmodelPlayerModel:GetChildren()) do
if v:IsA("Accessory") then
v:Destroy()
end
end
end
end
local function removeShadows()
if not PlayerModel and ViewmodelPlayerModel then return end
for i,v in pairs(PlayerModel:GetChildren()) do
if v:IsA("Part") then
v.CastShadow = false
end
end
for i,v in pairs(ViewmodelPlayerModel:GetChildren()) do
if v:IsA("Part") then
v.CastShadow = false
end
end
end
local function setTransparency(transparency)
for i,v in pairs(PlayerModel:GetDescendants()) do
local limb = table.find(ignored_limbs, v.Name)
if v:IsA("Part") and v ~= PlayerModel.PrimaryPart and v.Name ~= "Head" and v.Name ~= "Left Arm" and v.Name ~= "Right Arm" then
v.Transparency = transparency
end
end
end
local function setTransparencyV(transparency)
for i,v in pairs(ViewmodelPlayerModel:GetDescendants()) do
local limb = table.find(ignored_limbs, v.Name)
if v:IsA("Part") and v ~= ViewmodelPlayerModel.PrimaryPart and v.Name ~= "Head" and v.Name ~= "Torso" and v.Name ~= "Left Leg" and v.Name ~= "Right Leg" then
v.Transparency = transparency
end
end
end
local function cameraOffset(head, rootpart, humanoid)
if cameraOffsets == false then
cameraOffsets = true
RunService:BindToRenderStep("CameraOffset", Enum.RenderPriority.Camera.Value-1, function()
if head and rootpart and humanoid then
humanoid.CameraOffset = (rootpart.CFrame+Vector3.new(0,.7,0)):pointToObjectSpace(head.CFrame.p)
end
end)
else
cameraOffsets = false
RunService:UnbindFromRenderStep("CameraOffset")
end
end
local function createnewcharacter()
if PlayerModel and ViewmodelPlayerModel or not script:WaitForChild("R6", .1) or not Character then return end
PlayerModel = script:WaitForChild("R6"):Clone()
ViewmodelPlayerModel = script:WaitForChild("R6V"):Clone()
addDescription()
return PlayerModel, ViewmodelPlayerModel
end
if PlayerModel == nil and ViewmodelPlayerModel == nil and Character then
createnewcharacter()
end
if PlayerModel and ViewmodelPlayerModel and Character and Character:WaitForChild("HumanoidRootPart") then
local angle, position
local rootpart2, torso, torsoAnimationC, leftlegAnimationC, rightlegAnimationC, headAnimationC, rightarmAnimationC, leftarmAnimationC
local torso2
local torsoAnimationVC
local head2 = PlayerModel:WaitForChild("Head", .1)
local head, rootpart = Character:WaitForChild("Head", .1), Character:WaitForChild("HumanoidRootPart", .1)
Humanoid = Character:WaitForChild("Humanoid", .1)
rootpart2 = PlayerModel:WaitForChild("HumanoidRootPart", .1)
torso = PlayerModel:WaitForChild("Torso", .1)
local rootpartV = ViewmodelPlayerModel:WaitForChild("HumanoidRootPart")
torso2 = ViewmodelPlayerModel:WaitForChild("Torso", .1)
torsoAnimationC = rootpart2:WaitForChild("RootJoint", .1)
torsoAnimationVC = rootpartV:WaitForChild("RootJoint", .1)
leftlegAnimationC = torso:WaitForChild("Left Hip", .1)
rightlegAnimationC = torso:WaitForChild("Right Hip", .1)
headAnimationC = torso:WaitForChild("Neck", .1)
rightarmAnimationC = torso2:WaitForChild("Right Shoulder", .1)
leftarmAnimationC = torso2:WaitForChild("Left Shoulder", .1)
PlayerModel.Parent = Camera
ViewmodelPlayerModel.Parent = Camera
if Character.PrimaryPart and rootpart2 then
rootpart2:WaitForChild("RootJoint").Transform = Character.PrimaryPart:WaitForChild("RootJoint").Transform
rootpart2:WaitForChild("RootJoint").C0 = Character.PrimaryPart:WaitForChild("RootJoint").C0
position = (Character.PrimaryPart.CFrame)
position *= cf(0, 0, math.asin(DistanceFromRealBody.Value))
if Character:WaitForChild("Torso", .1) and Character:WaitForChild("Torso"):WaitForChild("Left Hip", .1) and PlayerModel:WaitForChild("Torso") and PlayerModel:WaitForChild("Torso"):WaitForChild("Left Hip") and leftlegAnimationC then
leftlegAnimationC.Transform = Character:WaitForChild("Torso"):WaitForChild("Left Hip").Transform
torso:WaitForChild("Left Hip").C0 = Character:WaitForChild("Torso"):WaitForChild("Left Hip").C0
end
if Character:WaitForChild("Torso", .1) and Character:WaitForChild("Torso"):WaitForChild("Right Hip", .1) and PlayerModel:WaitForChild("Torso") and PlayerModel:WaitForChild("Torso"):WaitForChild("Right Hip") and rightlegAnimationC then
rightlegAnimationC.Transform = Character:WaitForChild("Torso"):WaitForChild("Right Hip").Transform
torso:WaitForChild("Right Hip").C0 = Character:WaitForChild("Torso"):WaitForChild("Right Hip").C0
end
if Character:WaitForChild("Torso", .1) and Character:WaitForChild("Torso"):WaitForChild("Neck", .1) and PlayerModel:WaitForChild("Torso") and PlayerModel:WaitForChild("Torso"):WaitForChild("Neck") and headAnimationC then
headAnimationC.Transform = Character:WaitForChild("Torso"):WaitForChild("Neck").Transform
torso:WaitForChild("Neck").C0 = Character:WaitForChild("Torso"):WaitForChild("Neck").C0
end
if isInFirstPerson() then
setTransparencyV(0)
setTransparency(0)
-- VM
task.spawn(function()
if ViewmodelPlayerModel.PrimaryPart and PlayerModel.PrimaryPart and Character then
if Character:WaitForChild("Torso", .1) and Character:WaitForChild("Torso"):WaitForChild("Left Shoulder", .1) and ViewmodelPlayerModel:WaitForChild("Torso") and ViewmodelPlayerModel:WaitForChild("Torso"):WaitForChild("Left Shoulder") and leftarmAnimationC then
leftarmAnimationC.Transform = Character:WaitForChild("Torso"):WaitForChild("Left Shoulder").Transform
torso2:WaitForChild("Left Shoulder").C0 = Character:WaitForChild("Torso"):WaitForChild("Left Shoulder").C0
end
if Character:WaitForChild("Torso", .1) and Character:WaitForChild("Torso"):WaitForChild("Right Shoulder", .1) and ViewmodelPlayerModel:WaitForChild("Torso") and ViewmodelPlayerModel:WaitForChild("Torso"):WaitForChild("Right Shoulder") and rightarmAnimationC then
rightarmAnimationC.Transform = Character:WaitForChild("Torso"):WaitForChild("Right Shoulder").Transform
torso2:WaitForChild("Right Shoulder").C0 = Character:WaitForChild("Torso"):WaitForChild("Right Shoulder").C0
end
if welded then
updatePos(ViewmodelPlayerModel.PrimaryPart)
else
rootpartV:WaitForChild("RootJoint").Transform = Character.PrimaryPart:WaitForChild("RootJoint").Transform
rootpartV:WaitForChild("RootJoint").C0 = Character.PrimaryPart:WaitForChild("RootJoint").C0
local pos = (Character.PrimaryPart.CFrame)
pos *= cf(0, 0, math.asin(DistanceFromRealBody.Value))
lerpPlayerV(pos)
end
end
end)
-- Body
task.spawn(function()
if not cameraOffsets then
cameraOffset(head, rootpart, Humanoid)
end
if PlayerModel.PrimaryPart then
local pos = (Character.PrimaryPart.CFrame)
pos *= cf(0, 0, math.asin(DistanceFromRealBody.Value))
lerpPlayer(pos)
end
end)
else
if cameraOffsets then
cameraOffset(head, rootpart, Humanoid)
end
if Humanoid then
Humanoid.CameraOffset = Vector3.new(0,0,0)
end
setTransparency(1)
setTransparencyV(1)
end
end
end
end)