So, lemme get this straight

What im trying to achieve is a viewmodel that replicates real arm animations and if you were holding a tool, it would copy those anims as well, i have edited and shortened this code down a lot but its still completely bugged, i have never worked with viewmodels before so this is my first attempt

The problems are the following:

my code has functions basically which run when certain conditions are true, i have one on tool equip in first person, one for tool equip in first person and one for tool unequip in first person which are binded to renderstepped

http://imgur.com/a/vfzVUYN

(what happens when i equip the tool in third person)

http://imgur.com/a/BRVINje

(what it looks like when i equip in third person then go into first person)

when i go into first person after equipping and then reequipping in third person the tool falls through the world, and when i reequip it in first person it flings me to a position then goes back to normal, kinda like how when you equip a tool thats anchored, but keep in mind my tool is cancollide off and unanchored also i get insane lag after reequipping in third person im really stumped on this

also, if you need to this is what the viewmodel looks like

https://imgur.com/a/khRhKkU

code :

local LocalPlayer = game:GetService("Players").LocalPlayer
local character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local RenderStepped = game:GetService("RunService").RenderStepped
local ViewModel = game:GetService("ReplicatedStorage").ViewModel:Clone()
local ViewModelRoot = ViewModel.PrimaryPart
local shirt = ViewModel:WaitForChild('Shirt')
local charshirt = character:WaitForChild('Shirt')
local cam = workspace.CurrentCamera
--apply shirt
shirt.ShirtTemplate = charshirt.ShirtTemplate

local bodyColors = ViewModel["Body Colors"]
local bodyColor = character['Body Colors']
-- apply colours.
bodyColors.HeadColor3 = bodyColor.HeadColor3
bodyColors.LeftArmColor3 = bodyColor.LeftArmColor3
bodyColors.RightArmColor3 = bodyColor.RightArmColor3
bodyColors.TorsoColor3 = bodyColor.TorsoColor3

ViewModel.Parent = nil

local function isFirstPerson()
	if (character.Head.CFrame.p - cam.CFrame.p).magnitude < 1 then
		return true
	else
		return false
	end
end

local vmOn = Instance.new('BoolValue', ViewModel)
vmOn.Name = 'vmOn'

local toolEquipped = Instance.new('BoolValue', ViewModel)
toolEquipped.Name = 'toolEquipped'

function onToolEqFP()
	character['Right Arm']:WaitForChild('Handle').Part1 = nil
	rgrip = ViewModel['Right Arm'].Grip			
	--character['Right Arm']:WaitForChild('Handle').Part0 = ViewModel['Right Arm']
	rgrip.Part0 = ViewModel['Right Arm']
	rgrip.Part1 = character:FindFirstChildOfClass("Tool").Handle
	rgrip.C0 = ViewModelRoot.ToolGrip.C0 * CFrame.new(0,0,0)
end

function onToolUqFP()
	character['Right Arm']:WaitForChild('Handle').Part1 = character:FindFirstChildOfClass("Tool").Handle
	rgrip.Part0 = nil
	rgrip.Part1 = nil
end

function toolTP()
	character['Right Arm']:WaitForChild('Handle').Part0 = character['Right Arm']
	character['Right Arm']:WaitForChild('Handle').Part1 = character:FindFirstChildOfClass("Tool").Handle
end

local tinginhand = false

while wait() do
	local isFirstPerson = isFirstPerson()
	if isFirstPerson == true then
		vmOn.Value = true
		ViewModel.Parent = workspace.CurrentCamera
		RenderStepped:Connect(function()
			ViewModelRoot.CFrame = workspace.CurrentCamera.CFrame * CFrame.new(0, -2, -1)
			-- Hardcoded logic testMotor6D
			ViewModel.Motor6D["Left Shoulder"].Transform = character.Torso["Left Shoulder"].Transform --these few lines attach the viewmodel to camera and copy real arm movement
			ViewModel.Motor6D["Right Shoulder"].Transform = character.Torso["Right Shoulder"].Transform --
			if character:FindFirstChildOfClass("Tool")~= nil and isFirstPerson == true then
				toolEquipped = true
				onToolEqFP()
			elseif character:FindFirstChildOfClass("Tool")== nil and toolEquipped == true then
				toolEquipped = false
				onToolUqFP()
			elseif character:FindFirstChildOfClass("Tool")~= nil and isFirstPerson == false and tinginhand == false and vmOn.Value == false then
				tinginhand = true 
				toolTP()
			end
		end)
	elseif isFirstPerson == false then
		ViewModel.Parent = nil
		vmOn.Value = false
	end
end

2 comments

0

Make sure the viewmodel's arms have the CanCollide property off. You should also not be creating a new connection every time the player is in first person. The code is very poorly optimized. Instead of using a while wait() loop just use RenderStepped.

0

CanCollide as well as anchored is all off, could you give me some tips on optimizing the code maybe? other than the renderstepped thing