Hey! I’m making a viewmodel script and want to make the viewmodel move the arm like its running when they move. I am not an animator, so i am wondering how i could do it without animation, if that’s possible.
Here’s the script:
local runService = game:GetService("RunService")
local player = game.Players.LocalPlayer
local char = workspace:WaitForChild(player.Name)
local humanoid = char:WaitForChild("Humanoid")
local mouse = player:GetMouse()
local cam = workspace.Camera
local camera = workspace.CurrentCamera
local head = game.Players.LocalPlayer.Character:WaitForChild("Head")
local walkf = false
local equipped = false
local function DestoryVM()
equipped = false
if workspace.CurrentCamera:FindFirstChild("Viewmodel") then
workspace.CurrentCamera:FindFirstChild("Viewmodel"):Destroy()
end
end
local CanEquip = true
local function IsFirstPerson()
if player.CameraMode == Enum.CameraMode.LockFirstPerson then
return true
else
return false
end
end
local function MakeViewModel()
if workspace.CurrentCamera:FindFirstChild("Viewmodel") or equipped == true or CanEquip == false then return end
local physicsService = game:GetService("PhysicsService")
local arms = game.ReplicatedStorage.ArmViewModel:Clone()
arms.Parent = workspace.CurrentCamera
arms.Name = "Viewmodel"
equipped = true
wait(0.1)
local SwayEffect = 0.75
local SwayCFrame = CFrame.new()
local LastCameraCFrame = CFrame.new()
runService.RenderStepped:Connect(function()
if not equipped then
return
end
local Rotation = workspace.CurrentCamera.CFrame:ToObjectSpace(LastCameraCFrame)
local X,Y,Z = Rotation:ToOrientation()
SwayCFrame = SwayCFrame:Lerp(CFrame.Angles(math.sin(X) * SwayEffect, math.sin(Y) * SwayEffect, 0), 0.1)
LastCameraCFrame = workspace.CurrentCamera.CFrame
if arms.PrimaryPart then
arms:SetPrimaryPartCFrame(cam.CFrame *SwayCFrame * CFrame.new(0,-1,-1))
end
end)
end
--running
game:GetService("RunService").Heartbeat:Connect(function()
if IsFirstPerson() then
MakeViewModel()
else
DestoryVM()
end
end)
local Running = false
humanoid.Died:Connect(function()
equipped = false
CanEquip = false
end)
2 Likes
bumping this topic, as i’ve still not gotten an reply.
Hi so I know this has been over a year since you have made this post but I have created a decent system for the view model running system. The way it works is every Renderstepped (Physics frame rendered) it checks if the player is moving or not. And if the player is moving it then detects wether the player is running or walking. Once it determines everything it then plays the Animate Viewmodel function. The function makes the view model transparent when idle or walking so it does not obstruct the view of the camera but once the player is Running it then plays the running animation which is found in the Animations folder of the view model.
Here is a Demo Video I have made inside my own test place.
And here is the code! I hope this helps!
local player = game.Players.LocalPlayer
local char = workspace:WaitForChild(player.Name)
local humanoid = char:WaitForChild("Humanoid")
local RunService = game:GetService("RunService")
local mouse = player:GetMouse()
local cam = workspace.Camera
local camera = workspace.CurrentCamera
local head = game.Players.LocalPlayer.Character:WaitForChild("Head")
local CurrentAnim = "Idle"
local walkf = false
local equipped = false
local function DestoryVM()
equipped = false
if workspace.CurrentCamera:FindFirstChild("Viewmodel") then
workspace.CurrentCamera:FindFirstChild("Viewmodel"):Destroy()
end
end
local CanEquip = true
local function IsFirstPerson()
if player.CameraMode == Enum.CameraMode.LockFirstPerson then
return true
else
return false
end
end
local function MakeViewModel()
if workspace.CurrentCamera:FindFirstChild("Viewmodel") or equipped == true or CanEquip == false then return end
local physicsService = game:GetService("PhysicsService")
local Character = player.Character
local arms = game.ReplicatedStorage.ViewModel:Clone()
local ShirtTexture = Character:WaitForChild("Shirt"):Clone()
ShirtTexture.Parent = arms
local SkinColor = Character:WaitForChild("Body Colors"):Clone()
SkinColor = arms
arms.Parent = workspace.CurrentCamera
arms.Name = "Viewmodel"
equipped = true
wait(0.1)
local SwayEffect = 0.75
local SwayCFrame = CFrame.new()
local LastCameraCFrame = CFrame.new()
runService.RenderStepped:Connect(function()
if not equipped then
return
end
local Rotation = workspace.CurrentCamera.CFrame:ToObjectSpace(LastCameraCFrame)
local X,Y,Z = Rotation:ToOrientation()
SwayCFrame = SwayCFrame:Lerp(CFrame.Angles(math.sin(X) * SwayEffect, math.sin(Y) * SwayEffect, 0), 0.1)
LastCameraCFrame = workspace.CurrentCamera.CFrame
if arms.PrimaryPart then
arms:SetPrimaryPartCFrame(cam.CFrame *SwayCFrame * CFrame.new(0,-1,-1))
end
end)
end
local function animateVM(status)
if not equipped then
return
end
local animator = workspace.CurrentCamera:WaitForChild("Viewmodel").Humanoid.Animator
local animations = workspace.CurrentCamera:WaitForChild("Viewmodel").Animations
if CurrentAnim == "Idle" then
workspace.CurrentCamera:WaitForChild("Viewmodel"):WaitForChild("Left Arm").Transparency = 1
workspace.CurrentCamera:WaitForChild("Viewmodel"):WaitForChild("Right Arm").Transparency = 1
elseif CurrentAnim == "Run" then
workspace.CurrentCamera:WaitForChild("Viewmodel"):WaitForChild("Left Arm").Transparency = 0
workspace.CurrentCamera:WaitForChild("Viewmodel"):WaitForChild("Right Arm").Transparency = 0
end
if animator then
if status == "Idle" then
if CurrentAnim ~= "Idle" then
for i,v in pairs(workspace.CurrentCamera:WaitForChild("Viewmodel").Humanoid:GetPlayingAnimationTracks()) do
v:Stop()
end
animator:LoadAnimation(animations.Idle):Play()
CurrentAnim = "Idle"
end
elseif status == "Run" then
if humanoid.WalkSpeed == 10 then
if CurrentAnim ~= "Idle" then
for i,v in pairs(workspace.CurrentCamera:WaitForChild("Viewmodel").Humanoid:GetPlayingAnimationTracks()) do
v:Stop()
end
animator:LoadAnimation(animations.Idle):Play()
CurrentAnim = "Idle"
end
end
--[[if CurrentAnim ~= "Walk" then
for i,v in pairs(workspace.CurrentCamera:WaitForChild("Viewmodel").Humanoid:GetPlayingAnimationTracks()) do
v:Stop()
end
animator:LoadAnimation(animations.Walk):Play()
CurrentAnim = "Walk"
end]]
if humanoid.WalkSpeed == 21 then
if CurrentAnim ~= "Run" then
for i,v in pairs(workspace.CurrentCamera:WaitForChild("Viewmodel").Humanoid:GetPlayingAnimationTracks()) do
v:Stop()
end
animator:LoadAnimation(animations.Run):Play()
CurrentAnim = "Run"
end
end
end
end
end
--running
game:GetService("RunService").Heartbeat:Connect(function()
if IsFirstPerson() then
MakeViewModel()
else
DestoryVM()
end
end)
local function IsPlayerMoving() --function
if humanoid.MoveDirection.Magnitude == 0 then --if the movedirection is equal to 0 then the following will happen;
animateVM("Idle")
elseif humanoid.MoveDirection.Magnitude > 0 then --if the movedirection is greater than 0 then the following will happen;
animateVM("Run")
end
end
RunService.RenderStepped:Connect(IsPlayerMoving)
local Running = false
humanoid.Died:Connect(function()
DestoryVM()
end)
2 Likes