Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to make this viewmodel appear when a certain tool is equipped?

So i made this viewmodel and then i put it in the camera but i want the viewmodel to only appear when a tool is equipped. I know it has something to do with "Tool.Equipped:Connect(function()" but I dont know how to put it together.

Script:

local cam = workspace.CurrentCamera
local run = game:GetService("RunService")
local reps = game:GetService("ReplicatedStorage")
local model = reps:WaitForChild("VSS ViewModel"):Clone()

for i,v in pairs (model:GetChildren()) do
if v:IsA("BasePart") then
if v ~= model.PrimaryPart then
local weld = Instance.new("Weld")
weld.Part0 = model.PrimaryPart
weld.Part1 = v
weld.C0 = model.PrimaryPart.CFrame:inverse()
weld.C1 = v.CFrame:inverse()
weld.Name = v.Name
weld.Parent = model.PrimaryPart
  end
 end
end

model.Parent = cam

run.RenderStepped:connect(function()
model:SetPrimaryPartCFrame(cam.CFrame *CFrame.new(0,-1.5, 1.5)) -- This is the position goes by X,Y,Z format
end)

1 answer

✓ Accepted answer

First of all, delete that weld thing. Its useless (if you want the weapon to appear on your screen).

Now, you need a tool that will place the model to your camera, right? Easy.

You have the "Equipped" event that will fire when a player quipts his tool.

Lets go over some steps:

  1. Make your tool. Insert a tool inside workspace and put a part inside it named "Handle". Make it transparent.

  2. Take your script and put it inside the tool. Delete the weld thing because its pretty much useless.

  3. Simply add "Equipped" event.

local tool = script.Parent

tool.Equipped:Connect(function()

end)
  1. You need to add the "RenderStepped" inside the equipped event. Like this:
gun.Parent = cam

tool.Equipped:Connect(function()
	run.RenderStepped:connect(function()
	model:SetPrimaryPartCFrame(cam.CFrame * CFrame.new(0,-1.5, 1.5)) 
	end)
end)

I am creating an FPS game too. Trust me, I gained a lot of knowledge ever since I started playing with this whole thing. If you want you can message me on ROBLOX and I will help you. Since I think we both have the same goal I guess.

7 comments

0

Thanks!

0

I recommend disconnecting the event on unequiped.

0

i like this explanation however the viewmodel still isn't becoming visible when the tool is equipped. Whenever i delete the weld the script just stops working completely

0

Nevermind I fixed it by just keeping the weld. Again thanks for this explanation.

0

you are using "Local" not "local" and the :connect is deprecated. use :Connect.

0

You dont really need your weld script, its actually useless. I tested it. I forgot to add something to my script. Ill add it right now and fix it. Thanks.

0

You need to parent the gun to your camera, I forgot to add that line.

This discussion is preserved as it was. New answers, comments, and votes are closed.