I have a script to add a tool to the viewmodel. But sometimes, the tool disappears. Here is the code:
Server
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local item = {}
item.init = function()
print("Drone Initiation")
end
item.use = function(player)
print("Starting Drone for "..tostring(player.Name))
end
item.equip = function(player:Player)
print("equiped")
local Character = player.Character
local equipAnimation = ReplicatedStorage.ItemAnimations:WaitForChild("droneController_viewModel")
local Animator:Animator = Character.Humanoid.Animator
local equipTrack = Animator:LoadAnimation(equipAnimation)
equipTrack.Name = "droneController_viewModel"
local tool = ReplicatedStorage.Items:WaitForChild("droneController"):Clone()
tool.Parent = Character
ReplicatedStorage.addVMTool:FireClient(player, tool)
equipTrack:Play()
return equipTrack
end
item.unequip = function(player:Player)
print("unequiping")
end
return item
Client:
ReplicatedStorage.addVMTool.OnClientEvent:Connect(function(model)
model.Parent = camera:WaitForChild("viewModel")
local viewModel = camera:WaitForChild("viewModel")
local toolGrip = Instance.new("Motor6D")
toolGrip.Name = "toolGrip"
toolGrip.Parent = viewModel
toolGrip.Part0 = viewModel.Torso
toolGrip.Part1 = model.root
toolGrip.Parent = viewModel.Torso
task.wait(0.4)
end)
local HttpService = game:GetService("HttpService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local Workspace = game:GetService("Workspace")
local camera = Workspace.CurrentCamera
local cameraclass = {}
cameraclass.__index = cameraclass
export type gameCamera = { camera: Camera, player: Player, cameraclass: initialize, viewport: Model }
function cameraclass.new(): gameCamera
local self: gameCamera = setmetatable({}, cameraclass)
local camera = Workspace.CurrentCamera
local Player = Players.LocalPlayer
self.camera = camera
self.player = Player
self.viewmodel = nil
return self
end
function cameraclass:__tostring()
return "player: " .. self.player.Name
end
function cameraclass:initialize()
self.player.CameraMode = Enum.CameraMode.LockFirstPerson
local character: Model = self.player.Character
print(HttpService:JSONDecode(self))
if not self.player.Character then
self.player.CharacterAdded:Wait()
end
print("player: " .. self.player.Character.Name)
self.player.Character.Archivable = true
--local RunService = game:GetService("RunService")
--local PhysicsService = game:GetService("PhysicsService")
local Camera = workspace.CurrentCamera
if Camera:FindFirstChild("ViewModel") then
Camera.ViewModel:Destroy()
end
local Animator: Animator = character:WaitForChild("Humanoid"):WaitForChild("Animator")
local ViewModel = ReplicatedStorage.viewModel:Clone()
local BodyColors = character:FindFirstChildOfClass("BodyColors"):Clone()
BodyColors.Parent = ViewModel
ViewModel.Parent = camera
local ViewModelAnimator = ViewModel.Humanoid:WaitForChild("Animator")
Animator.AnimationPlayed:Connect(function(AnimationTrack)
print("loading")
local track = ViewModelAnimator:LoadAnimation(AnimationTrack.Animation)
track.Priority = Enum.AnimationPriority.Action4
track:Play()
end)
ViewModel:PivotTo(character.Head.CFrame)
self.viewModelConnection = RunService.RenderStepped:Connect(function()
ViewModel.PrimaryPart.CFrame = camera.CFrame
end)
self.viewmodel = ViewModel
end
return cameraclass