You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? Keep it simple and clear!
I am following a ViewModel tutorial made by @rokoblox5 (Link). When I finished it, I found the problem as said as the title. I don’t why is it like that. -
What is the issue? Include screenshots / videos if possible!
Just like the title said, “Gun disappear after few seconds when equipped” -
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I already look for the solution in Devforum, “Tool Disappear after equipped”. But it doesn’t help me. I don’t know if it is the script’s problem.
Here are the scripts:
Local Script (Tool):
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
if not Character.Parent then
Character = Player.CharacterAdded:Wait()
end
local Animator = Character:WaitForChild("Humanoid"):WaitForChild("Animator")
local Animation = Instance.new("Animation", script.Parent)
Animation.AnimationId = "rbxassetid://11366011046"
Animation = Animator:LoadAnimation(Animation)
Animation:SetAttribute("ViewModelAnimation", true)
script.Parent.Equipped:Connect(function()
Animation:Play()
end)
script.Parent.Unequipped:Connect(function()
Animation:Stop()
end)
Local Script (StarterCharacterScript):
local RunService = game:GetService("RunService")
local PhysicsService = game:GetService("PhysicsService")
local Camera = workspace.CurrentCamera
if Camera:FindFirstChild("ViewModel") then
Camera.ViewModel:Destroy()
end
local Character = script.Parent
local Animator = Character:WaitForChild("Humanoid"):WaitForChild("Animator")
Character.Archivable = true
local ViewModel = Character:Clone()
ViewModel.Parent = Camera
ViewModel.Name = "ViewModel"
Character.Archivable = false
local ViewModelAnimator = ViewModel.Humanoid.Animator
ViewModel.Humanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None
ViewModel.Humanoid.HealthDisplayType = Enum.HumanoidHealthDisplayType.AlwaysOff
ViewModel.Humanoid.BreakJointsOnDeath = false
ViewModel.Head.Anchored = true
ViewModel.PrimaryPart = ViewModel.Head
ViewModel:SetPrimaryPartCFrame(CFrame.new(0,5,10))
for _, part in pairs(ViewModel:GetDescendants()) do
if part:IsA("BasePart") then
part.CastShadow = false
PhysicsService:SetPartCollisionGroup(part, "ViewModel")
local LowerName = part.Name:lower()
if LowerName:match("leg") or LowerName:match("foot") then
part:Destroy()
elseif not (LowerName:match("arm") or LowerName:match("hand")) then
part.Transparency = 1
end
elseif part:IsA("Decal") then
part:Destroy()
elseif part:IsA("Accessory") then
part:Destroy()
elseif part:IsA("LocalScript") then
part:Destroy()
end
end
local LoadedAnimatons = {}
Animator.AnimationPlayed:Connect(function(animationTrack)
if animationTrack:GetAttribute("ViewModelAnimation") == false then
return
end
if not LoadedAnimatons[animationTrack] then
LoadedAnimatons[animationTrack] = ViewModelAnimator:LoadAnimation(animationTrack.Animation)
end
end)
local function UpdateAnimations()
for CharAnim, Anim in pairs(LoadedAnimatons) do
if CharAnim.IsPlaying ~= Anim.IsPlaying then
if CharAnim.IsPlaying then
Anim:Play()
else
Anim:Stop()
end
end
Anim.TimePosition = CharAnim.TimePosition
Anim:AdjustWeight(CharAnim.WeightCurrent, 0)
end
end
Character.DescendantAdded:Connect(function(Obj)
if Obj:IsA("Weld") and Obj.Name == "RightGrip" then
task.wait()
Obj.Part0 = ViewModel[Obj.Part0.Name]
end
end)
RunService.RenderStepped:Connect(function(deltaTime)
UpdateAnimations()
ViewModel:SetPrimaryPartCFrame(Camera.CFrame)
local ViewModelShirt = ViewModel:FindFirstChildWhichIsA("Shirt") or Instance.new("Shirt", ViewModel) -- // Create a shirt if there is no shirt found.
local CharacterShirt = Character:FindFirstChildWhichIsA("Shirt")
if CharacterShirt then
-- // If a shirt was found in the player's character, then set the ViewModel's shirt to the same shirt.
ViewModelShirt.ShirtTemplate = CharacterShirt.ShirtTemplate
end
for _, Part in pairs(ViewModel:GetChildren()) do
if Part:IsA("BasePart") then
-- // Set the color of each part of the ViewModel to the color of the part with same name in the character.
Part.Color = Character[Part.Name].Color
end
end
end)
I wish someone can help me solve this problem. Besides, the FPS zombie game is one of my favourite games to play. So, I wish I can make one too.
Thank you!