Ad
Log in to vote
1

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

Asked by
exarlus 57
12 days ago

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

Log in to vote
0
Answered by 12 days ago
Edited 12 days ago

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)

4) 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.

0
Thanks! exarlus 57 — 12d
0
I recommend disconnecting the event on unequiped. Phlegethon5778 171 — 12d
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 exarlus 57 — 12d
0
Nevermind I fixed it by just keeping the weld. Again thanks for this explanation. exarlus 57 — 12d
View all comments (3 more)
0
you are using "Local" not "local" and the :connect is deprecated. use :Connect. yHasteeD 1263 — 12d
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. HeyItzDanniee 149 — 12d
0
You need to parent the gun to your camera, I forgot to add that line. HeyItzDanniee 149 — 12d
Ad

Answer this question