I want to create first person movement for my weapons. I don’t know entirely how to do it (Example: Arsenal FPS, Phantom Forces FPS, Criminality FPS, etc.)
I thought of using viewport frames, but I couldn’t find any resources on that, and so far the only code I’ve seen through free models is just a bunch of junk I can’t even begin to figure out, and I want to make it myself, so when I go to fix something, I know how to fix it. And I can create anything I want.
I’ve tried to look up tutorials on it, but its just either a gun system that confuses me, or some sort of free model to just download and insert.
This is normally done by positioning the arms/weapon relative to the camera.
Run this code in the command bar or a local script:
local part = Instance.new("Part")
part.Parent = workspace
game:GetService("RunService").RenderStepped:Connect(function()
part.CFrame = workspace.CurrentCamera.CFrame * CFrame.new(3, -4, -9)
end)
A gray part will stay fixed to your screen somewhere to the bottom right.
The next step after this is to make that script place some arms and a gun in front of your camera instead of just a gray brick and to animate the hands/gun in response to firing, reloading, swinging the camera etc. and to account for the aspect ratio and field of view.
You don’t have to use Animations. Tweens or CFraming will suffice.
But Humanoid Animations are a smart choice if you can get them to work because you can create their animations with an animation editor instead of writing them in code.
I actually got animations to work. But I can’t hide them when I unequip a tool of some sort. For example:
When I equip them, it clones a model from replicated first.
BUT: When I unequip, a bunch of errors come up, and I don’t know how to fix them.
12:10:13.765 ▶ Model:SetPrimaryPartCFrame() failed because no PrimaryPart has been set, or the PrimaryPart no longer exists. Please set Model.PrimaryPart before using this. (x74) - Client - LocalScript:10
12:10:14.997 The Parent property of Viewmodel is locked, current parent: NULL, new parent Workspace - Client - LocalScript:7
12:10:14.998 ▶ Model:SetPrimaryPartCFrame() failed because no PrimaryPart has been set, or the PrimaryPart no longer exists. Please set Model.PrimaryPart before using this. (x75) - Client - LocalScript:10
localscript:
local tool = script.Parent
local model = game:GetService("ReplicatedFirst").Viewmodel:Clone()
tool.Equipped:Connect(function()
model.Parent = workspace
game:GetService("RunService").RenderStepped:Connect(function()
model:SetPrimaryPartCFrame(workspace.CurrentCamera.CFrame * CFrame.new(0, 0, 0))
end)
model.Humanoid.Animator:LoadAnimation(game:GetService("ReplicatedFirst").Viewmodel.Equip):Play()
end)
tool.Unequipped:Connect(function()
model:Destroy()
end)
Seems like you’re simply not cleaning up after yourself. The RenderStepped event is still running even after the Model is deleted.
local tool = script.Parent
local model = game:GetService("ReplicatedFirst").Viewmodel:Clone()
-- added
local connection
tool.Equipped:Connect(function()
model.Parent = workspace
-- added `connection = `
connection = game:GetService("RunService").RenderStepped:Connect(function()
model:SetPrimaryPartCFrame(workspace.CurrentCamera.CFrame * CFrame.new(0, 0, 0))
end)
model.Humanoid.Animator:LoadAnimation(game:GetService("ReplicatedFirst").Viewmodel.Equip):Play()
end)
tool.Unequipped:Connect(function()
-- added
connection:Disconnect() -- prevent that function in RenderStepped from running again and printing errors
model:Destroy()
end)
edit: this will not work if you equip it a second time after unequipping because model is destroyed; instead, set model.Parent = nil
Yes. Anything that you can Connect(), you can later disconnect if you hang on to the return value. Event:Connect(func) will return a Connection, and you can Connection:Disconnect() to prevent the event from running again.
Part touch events and such can be disconnected too.
You can also Event:Wait() to wait until the next time the event runs instead of connecting and disconnecting immediately, but this isn’t relevant here.
Ohhhh yea I forgot about the parenting… I changed it so that way it waits for a model named MODEL because I cloned the model and named it MODEL, then it destroys it when I unequip, but it comes up with this error: