local ReplicatedStorage = game:GetService("ReplicatedStorage")
local viewModels = ReplicatedStorage:WaitForChild("fpsViewModels"):GetChildren();
local cam = workspace.Camera
local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local IdleAnimation = Instance.new("Animation")
IdleAnimation.AnimationId = "rbxassetid://7120507317"
local currentWeapon
local currentIndex
local viewModelHumanoid
local function Equip(p_int)
viewModels[p_int]:SetPrimaryPartCFrame(cam.CFrame * CFrame.new(0,0,0.5))
currentIndex = p_int
currentWeapon = viewModels[currentIndex]
viewModelHumanoid = currentWeapon:WaitForChild("Humanoid")
local AnimationTrack = viewModelHumanoid:LoadAnimation(IdleAnimation)
AnimationTrack:Play()
currentWeapon.Parent = workspace
end
RunService.RenderStepped:Connect(function()
for i = 1,#viewModels do
Equip(i)
end
end)
UIS.InputBegan:Connect(function(Input)
if currentWeapon == workspace then
if Input.UserInputType == Enum.UserInputType.MouseButton1 then
print("Punching")
end
end
end)
I don’t think you need to use a Run Service function to run the function for each view model, as that makes the function run every frame. Instead just do the loop without the run function.
I think you should just loop, call the function and make the animation looped instead of using a RenderStepped event.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local viewModels = ReplicatedStorage:WaitForChild("fpsViewModels"):GetChildren();
local cam = workspace.Camera
local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local IdleAnimation = Instance.new("Animation")
IdleAnimation.AnimationId = "rbxassetid://7120507317"
local currentWeapon
local currentIndex
local viewModelHumanoid
local function Equip(p_int)
viewModels[p_int]:SetPrimaryPartCFrame(cam.CFrame * CFrame.new(0,0,0.5))
currentIndex = p_int
currentWeapon = viewModels[currentIndex]
viewModelHumanoid = currentWeapon:WaitForChild("Humanoid")
local AnimationTrack = viewModelHumanoid:LoadAnimation(IdleAnimation)
AnimationTrack.Looped = true
AnimationTrack:Play()
currentWeapon.Parent = workspace
end
UIS.InputBegan:Connect(function(Input)
if currentWeapon == workspace then
if Input.UserInputType == Enum.UserInputType.MouseButton1 then
print("Punching")
end
end
end)
for i = 1,#viewModels do
Equip(i)
end