How do you change bodygroups on a weapon's viewmodel?

hey i made a weapon’s viewmodel with more than one body groups, but i cant figure out how to change the body group of the weapon’s viewmodel so can anyone help me out. i know self:SetBodygroup doesnt work and i image that works for worldmodels

Can we get to see the code?
Make sure to check that you are using the correct bodygroup ID using https://wiki.facepunch.com/gmod/Entity:GetBodyGroups
Quick reminder that they start at 0 and not 1.

here are the bodygroups i added

$bodygroup “studio”
{
studio “1_reference.smd”
studio “2_reference.smd”
}
$bodygroup “mag”
{
studio “1_mag.smd”
studio “2_mag.smd”
}
$bodygroup “barrel”
{
studio “1_barrel.smd”
studio “2_barrel.smd”
}
$bodygroup “grip”
{
studio “grip5.smd”
}
$bodygroup “stock”
{
studio “1_stock.smd”
studio “2_stock.smd”
blank
}

i want to be able to change them when ever i thought putting SetBodygroup in the swep reload so i have a button to change them, but nothing would happen with self:SetBodygroup so i figured oh that would change the world model and i have no idea how you would change the viewmodel and i tried googling it and didnt really get anything so im here now. showing code isnt much it was setbodygroup(1,1) (i did that for all 4 bodygroups it said i had) in swep:reload realising that wouldnt work and have no idea on how to call for it to change the view model.

so any help? as i would love to beable to get this done and thank you for reading

Well, if you are calling self:SetBodyGroup() inside of the SWEP:Reload() nothing will happen because you are calling the function on the weapon entity, the viewmodel is a different entity.
You would need to get the viewmodel itself using https://wiki.facepunch.com/gmod/Player:GetViewModel

Something like this:

function SWEP:Reload()

	local viewModel = self.Owner:GetViewModel()
	viewModel:SetBodygroup(1, 1)

end
function SWEP:Reload()
if CLIENT then 
	local viewModel = self.Owner:GetViewModel()
	viewModel:SetBodygroup(0, 1)
	viewModel:SetBodygroup(1, 1)
	viewModel:SetBodygroup(2, 1)
end
end

works but it instantly changes back after i change it, like it just flashes for half a second. would i need to overwrite it in some way

ok never mind so if i do

function SWEP:Reload()

	local viewModel = self.Owner:GetViewModel()
	viewModel:SetBodygroup(1, 1)

end

it doesnt work and if i do

function SWEP:Reload()

	local viewModel = self.Owner:GetViewModel()
	viewModel:SetBodygroup(1, 1)

end

it doesnt work but if i do

function SWEP:Reload()

if CLIENT then 
	local viewModel = self.Owner:GetViewModel()
	viewModel:SetBodygroup(0, 1)
	viewModel:SetBodygroup(1, 1)
	viewModel:SetBodygroup(2, 1)
end

	local viewModel = self.Owner:GetViewModel()
	viewModel:SetBodygroup(0, 1)
	viewModel:SetBodygroup(1, 1)
	viewModel:SetBodygroup(2, 1)
end

it works lol so everything good now

That’s because you are calling it only on the client and not on the server. After some time passes, the server tells the client that the model is wrong and it updates. The viewmodel is an entity on the server’s side as well, so you can call it on shared.

ahh, im more used to coding mainly server side and or gui stuff not really client models but thank you for helping me out :smiley: now to make my system so the guns spawn body groups randomly and depending on the gun on the floor you pick up, your view model well match its body groups

This should help with that: https://wiki.facepunch.com/gmod/ENTITY:SetupDataTables
This is a good example of it in use: https://github.com/Facepunch/garrysmod/blob/master/garrysmod/lua/entities/sent_ball.lua
Good luck

ah yes i can use that to set the body groups and colour and such on the spawn function