How to make a viewmodel script that works with CAS

asd viewmodel.rbxm (105.8 KB)

Ok local script inside the starterplayerscript and viewmodel to the ReplictedStorage.
Things I did for the viewmodel:
I anchored it (so it wont drop doesnt really matter but in some cases it does)
Put cancollide in false (so it wont glitch the character)
Changed the Hands Motor6d to the arm ( easier for animating )

for the script:

local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local equipped = false

local ViewModel = game.ReplicatedStorage:WaitForChild("ViewModel"):Clone()
ViewModel.Parent = workspace.Camera

local ViewmodelHoldAnimation = Instance.new("Animation")
ViewmodelHoldAnimation.AnimationId = "ur animation id"

local HoldAnim = ViewModel:WaitForChild("Humanoid"):LoadAnimation(ViewmodelHoldAnimation)

RunService.RenderStepped:Connect(function()
	ViewModel:PivotTo(workspace.Camera.CFrame * CFrame.new(0,-1,-1)) -- sets the viewmodels position to the camera in a loop that runs every frame
end)

local function ToggleFlashlight() -- I wasnt sure why there was idle but I just deleted it you can change the script the way you want
	if not equipped then
		equipped = true
		HoldAnim:Play()
	else
		equipped = false
		HoldAnim:Stop()
	end
end
-- Used UIS instead of CAS because its useless in my opinion and this is easier
UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end 
	if input.KeyCode == Enum.KeyCode.F or input.KeyCode == Enum.KeyCode.ButtonY then
		ToggleFlashlight()
	end
end)

--[[ for mobile support you can use this idk 
gui.Activated:Connect(function()
	ToggleFlashlight()
end)
]]

Please put Solved if this fixes your issue. And improve your scripting skills this way you arent really learning something

Dont forget to ask any further questions too!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.