Still have questions? Join our Discord server and get real time help.
1

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

exarlus 70
4 months 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)



0
Edited 4 months 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.

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 70 — 4mo
0
I recommend disconnecting the event on unequiped. User#25115 0 — 4mo
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 70 — 4mo
0
Nevermind I fixed it by just keeping the weld. Again thanks for this explanation. exarlus 70 — 4mo