So I was working on a viewmodel swaying script for a game, but the script I tried wasn’t working properly, it just makes the viewmodel disappear. I have no idea as to why this is happening, and I haven’t found a solution to this problem yet, so if someone could show me how to fix the script, I’d be incredibly thankful!
Local script inside of a tool:
local plr = game.Players.LocalPlayer
local char = script.Parent.Parent
local cam = workspace.Camera
local run = game:GetService("RunService")
local viewModelCFrame = CFrame.new()
local currentCam = game.Workspace.CurrentCamera
local oldPos = CFrame.new(0,0,0)
local stopped=false
script.Parent.Equipped:Connect(function()
local arms = game.ReplicatedStorage.ViewModels.Arms:Clone()
local mult = 6
local lastCameraCF = cam.CFrame
local swayOffset = CFrame.new()
coroutine.wrap(function()
run:BindToRenderStep("CameraRender", Enum.RenderPriority.Camera.Value + 1, function()
local rotation = cam.CFrame:toObjectSpace(lastCameraCF) --get cframe delta.
local x,y,z = rotation:ToOrientation() --I'm sure there are better ways to get rotation but this will work for now.
swayOffset = swayOffset:Lerp(CFrame.Angles(math.sin(x)*mult,math.sin(y)*mult,0), 0.1) --calculate the sway using SIN
arms.PartHumanoid.CFrame = arms.PartHumanoid.CFrame * swayOffset --apply the sway
lastCameraCF = cam.CFrame --update the last cframe
end)
end)()
local connection = run.RenderStepped:Connect(function()
if game.Players.LocalPlayer.Character.Head.LocalTransparencyModifier == 1 then
arms.Parent = cam
script.Parent.Handle.LocalTransparencyModifier = 1
if game.Players.LocalPlayer.Character.Humanoid.MoveDirection.Magnitude > 0 then
local iTime = os.clock()
local bounce = math.sin(iTime*9)
if bounce >= 0 then
bounce = -bounce
end
viewModelCFrame = viewModelCFrame:Lerp(CFrame.new(math.cos(iTime*9),-1 + bounce, 0), 0.035)
arms:SetPrimaryPartCFrame(cam.CFrame * viewModelCFrame)
else
viewModelCFrame = viewModelCFrame:Lerp((CFrame.new(0,-2,0)),.1)
arms:SetPrimaryPartCFrame(cam.CFrame * viewModelCFrame)
end
if(cam.CFrame ~= oldPos) then
print("MOVED")
oldPos = cam.CFrame
arms:SetPrimaryPartCFrame(
cam.CFrame *
lastCameraCF
)
else
if(stopped==false) then
stopped=true
print("STOPPED")
end
end
else
arms:SetPrimaryPartCFrame(cam.CFrame * CFrame.new(0,-1000000,0))
script.Parent.Handle.LocalTransparencyModifier = 0
end
end)
script.Parent.Unequipped:Connect(function()
connection:Disconnect()
arms:Destroy()
end)
end)
WARING: THIS SCRIPT IS PROBABLY REALLY HACKY AND IS TERRIBLE TO READ.
Any help is appreciated!