When I insert a humanoid in a viewmodel, I start flying?

When I insert a humanoid into a viewModel im making, I randomly start flying around. It only happens when in first person. If I remove the humanoid everything works fine again.
Heres a video:
robloxapp-20210122-1158310.wmv (2.5 MB)

The games avatar type is set to R6, I dont know if that has anything to do with it.

And heres the viewModel:

image

And heres the viewModel script:

local FPS = {}
FPS.__index = FPS

local runService = game:GetService("RunService")
local UIS = game:GetService("UserInputService")
local CAS = game:GetService("ContextActionService")
local replicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")

local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local hrp = character:WaitForChild("HumanoidRootPart")

local camera = workspace.CurrentCamera

local remotes = replicatedStorage:WaitForChild("RemoteEvents")

-- FUNCTIONS

local function Setup(self)
	self.viewModel.Parent = camera
	print("setup parented to camera")
end

-- CLASS
function FPS.new()
	local self = {}
	self.viewModel = camera:WaitForChild("ViewModel")
	self.weapon = self.viewModel:FindFirstChildOfClass("Model")
	
	self.aiming = false
	self.equipped = true
	
	Setup(self)
	
	self.gunHoldAnim = humanoid:LoadAnimation(self.weapon.Animations.GunHoldAnim)
	self.gunAimAnim = humanoid:LoadAnimation(self.weapon.Animations.GunAimAnim)
	self.currentAnim = self.gunHoldAnim
	
	self.viewAnim_aim = self.viewModel.AnimationController:LoadAnimation(self.weapon.Animations.ViewModel_aim)
	
	return setmetatable(self, FPS)
end

function FPS:PlayerDeath()
	self.viewModel.Parent = nil
	local char = players.LocalPlayer.Character or players.LocalPlayer.CharacterAdded:Wait()
	humanoid = char:WaitForChild("Humanoid")
end

function FPS:GunSway()
	local currentCF = camera.CFrame
	local t = tick() * (3/4)

	local x = math.cos(t * 5) * 0.08
	local y = math.abs(math.sin(t * 5)) * 0.05

	local cf = currentCF * CFrame.new(x, y, 0)
	self.viewModel.HumanoidRootPart.CFrame = cf
end

function FPS:Equip()
	----
end

local fps = FPS.new()

local function GunAimBinding(actionName, inputState, inputObject)
	if inputState == Enum.UserInputState.Begin then
		replicatedStorage.RemoteEvents.GunEvents.Aiming:Fire(true)
		UIS.MouseIconEnabled = false
		fps.aiming = true
		fps.viewAnim_aim:Play()
	end
	if inputState == Enum.UserInputState.End then
		replicatedStorage.RemoteEvents.GunEvents.Aiming:Fire(false)
		UIS.MouseIconEnabled = true
		fps.aiming = false
		fps.viewAnim_aim:Stop()
	end
	return Enum.ContextActionResult.Pass
end

-- MAIN FUNCTION
local function Main()
	if fps.equipped then
		local temp = fps.viewModel:FindFirstChildOfClass("Model")
		fps.viewModel = camera:WaitForChild("ViewModel")
		fps.viewModel.HumanoidRootPart.CFrame = camera.CFrame
		local temp2 = fps.viewModel:FindFirstChildOfClass("Model")
		if humanoid.MoveDirection.Magnitude > 0 and not fps.aiming then
			fps:GunSway()
		end
	end
end

-- CONNECTIONSf
local connectDb = true
local connection
replicatedStorage.RemoteEvents.EquipTool.Event:Connect(function(tool)
	if connectDb == true then connectDb = false
		humanoid.Died:Connect(function() fps:PlayerDeath() end)
		local function onRender()
			local model = camera:FindFirstChild("ViewModel")
			if model then
				if model:FindFirstChild(tool.Name:upper()) then
					Main()
				else
					connectDb = true
					connection:Disconnect()
				end
			else
				connectDb = true
				connection:Disconnect()
			end
		end
		FPS.new()
		connection = runService.RenderStepped:Connect(onRender)
		CAS:BindAction('GunAim', GunAimBinding, true, Enum.UserInputType.MouseButton2)

		remotes.weaponSetup:FireServer(fps.viewModel:FindFirstChildOfClass("Model").Name) -- send weapon name
		remotes.custom:FireServer() -- empty custom event
		
		-- Clothing and Body Colors
		local bodyColors = character:FindFirstChildOfClass("BodyColors")
		local shirt = character:FindFirstChildOfClass("Shirt")
		
		if bodyColors and shirt then
			bodyColors:Clone().Parent = fps.viewModel
			shirt:Clone().Parent = fps.viewModel
		end
	end
end)
1 Like

Probably has to do with the fact that your Humanoid starts applying its own forces and hacks together collisions against the HumanoidRootPart. Have you perhaps tried disabling all the states of the Humanoid and placed it into a different collision group?

1 Like

Ok, I’ve never use collision groups before but it get this error: Parameter 1 must be BasePart in SetPartCollisionGroup.
Heres the script:

physicsService:CreateCollisionGroup("Humanoid")
physicsService:SetPartCollisionGroup(script.Parent, "Humanoid") -- script.Parent is the humanoid
physicsService:CollisionGroupSetCollidable("Humanoid", "Default", false)```

I’ve encountered this before and I know an easy fix. First go to colliongroups

then add a group and name it whatever like viewmodel image once you have the group created uncheck all the boxes in the viewmodel tab it should go from this image to this image next select both your viewmodels arms go to properties and change collisiongroupid to 1

3 Likes

CollisionGroupId is not a property of Humanoid, I’m trying to stop a humanoid from coliding not a Basepart

Actually, what is happening is if a humanoid is inserted, objects it will automatically force its cancollide to true.

I dont understand, do you know how I can stop the humanoid from colliding from other things

The humanoid itself is an instance, not an object. The thing that is colliding is the arms.

Yeah but when I remove the humanoid, I dont start flying anymore

thats because the humanoid forces parts cancollide to true

Ok, but how do I fix that though?

Use the guide that I originally posted.

Oh that fixed it, I was confused lol thanks

1 Like

Yeah, when I first made fps arms I was confused as well. Kind of annoying that it forces cancollide like that.

1 Like

Wait hold on, I’ve just added this and the arms CollisonGroupId is set to 1 but CanCollide is still on???
image

image

As you can see I’m glitching out again now

I recommend using an animationcontroller instead of a humanoid inside your viewmodel to combat these issues, it doesn’t force any collisions, keep the humanoid only inside the actual character itself.

but how to add clothes texture if you use animation controller?

you can but it really hard to do