Viewmodel Transparency Delay?

Hello! I am working on the viewmodel which is made to be played in first and third person view.

Whether I’m zooming, either first or third person view, it should be able to update viewmodel transparency immediately because I made it so it can update on every frame.

For some reason, I encounter an annoying problem in which when I zoom in first-person view, it works fine because the transparency immediately updates. But when I zoom out in third-person, it delays for a moment before making the viewmodel invisible.

I’m not sure if it’s me or the Roblox Studio bug itself. But I’ve been doing this for hours trying to fix this, but it’s no use. Mine is running very smoothly, around 60 FPS, no lag. Any help would be awesome!

Here’s the code, it’s a long code but I shorten them so it’ll be easier to understand what I’m focusing on.
Hope I don’t confuse your mind. Everything is working fine, it’s the only delaying part that’s bothering me.

Weapon Settings

local Settings = {
	acceptableclasses = {"Part", "MeshPart", "UnionOperation"} 
}

return Settings

LocalScript

local WeaponSettings = require(script.Parent:WaitForChild("Settings"))
local RS= game:GetService("RunService")
RS.RenderStepped:connect(function()
    local firsttable = {} --table for viewmodel, first person view
    local thirdtable = {} --talbe for tool player is holding, third person view

	for _, f in pairs(camera.FirstPersonBody:GetDescendants()) do --Find parts in viewmodel (first person view, then insert it into "firsttable.")
		for _, ac in pairs(WeaponSettings.acceptableclasses) do
			if f:IsA(ac) then
				table.insert(firsttable, f)
			end
		end
	end
	for _, t in pairs(script.Parent:GetDescendants()) do --Find parts from gun the player is holding (third person view, then insert it into "thirdtable.")
		for _, ac in pairs(WeaponSettings.acceptableclasses) do
			if t:IsA(ac) then
				table.insert(thirdtable, t)
			end
		end
	end

    if CameraDistance > 0.6 then --Distance between camera position and player's head
    --Third Person View
    	for _, f in pairs(firsttable) do
    		f.Transparency = 1
    	end
    	for _, t in pairs(thirdtable) do
    		t.Transparency = 0
   		end 
    else
    --First Person View
    	for _, f in pairs(firsttable) do
    		f.Transparency = 0
    	end
    	for _, t in pairs(thirdtable) do
    		t.Transparency = 1
    	end 
   	end
end)
1 Like

add a debounce to your loops.

Before doing the -- third person view, check if already in thirdperson so you don’t constantly run that for loop.

local isThird = true -- im guessing you start playing in third

...
   if not isThird then
      isThird = true
      for _, f ...
        ...
      end
   end

-- same for first person view

I think that since it’s runninng a for loop, the loop is finishing up the last routine and then the delay is caused from the running loop not allowing the update to take place as quickly.

You can improve performance and make this just three loops.

	local isThirdPerson = CameraDistance > 0.6

	for _, ac in pairs(WeaponSettings.acceptableclasses do --Find parts in viewmodel (first person view, then insert it into "firsttable.")
		for _, f in pairs(camera.firstpersonbody:GetDescendants()) do
    			if f:IsA(ac) then
    				f.Transparency = isThirdPerson and 1 or 0 -- you could also do another if statement
    			end
    		end
		for _, t in pairs(script.parent:GetDescendants()) do --Find parts from gun the player is holding (third person view, then insert it into "thirdtable.")
  			if t:IsA(ac) then
    				t.Transparency = isThirdPerson and 1 or 0 
 			end
		end
   	end

Looping through tons of instances every frame can’t be performant. If these instances are static, I’d instead just move the first two loops out of the connection and keep the four loops.

Edit: Three, not two.

1 Like

Hey y’all, sorry for late reply. Was figuring it out using the solution you gave.
I actually sort of fixed it, and then I came across to this part where the problem occurs.

Here, it show a cube and two viewmodel arms. Green is visible, and red is invisible.
When I zoomed in, both cube and two arms are suppose to change color to green at the same time, indicating in first-person view. But the arms sort of pauses then updates it after the cube’s color is changed.

But when I locate the tool inside my character in explorer tab, which functions as third-person view for other players to see, then set tool’s all meshparts’s transparency to 1, and tried zooming it again, the viewmodel arms are syncing with the cube now. Even when I bring up the graphics up to decrease frame rate, it still syncing it up, and that’s exactly what I wanted.

After like 8+ hours trying to find out, it boggles my mind over this. Anyway… I used meshparts to create a model for the tool. I think I started to notice that the more visible meshparts, the greater the delay. I’m not entirely sure if this is a case, but anybody got an idea why this is happening?

1 Like