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:
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)