STool not working.

Hey all. Can anyone work out why this STool isn’t working? It shows up in the menu fine, and selects the ragdolls fine, but then spams errors.

[lua]TOOL.Category = “Render”
TOOL.Name = “#Body Group Switcher”
TOOL.Command = nil
TOOL.ConfigName = nil

TOOL.ClientConVar[ “bodygroup” ] = 0

if ( SERVER ) then

local manualcontrol = true
local oldslider = 0
local newslider = 0

end

if ( CLIENT ) then

local BodyGroupswitcherPreviousEntity = nil

language.Add( “Tool_bodygroup_name”, “Body Group Changer” )
language.Add( “Tool_bodygroup_desc”, “Changes the Body Group an entity.” )
language.Add( “Tool_bodygroup_0”, “Left click to cycle body groups.
Right click to select a model for manipulation.
Reload to pick a random body group.”)

function TOOL.BuildCPanel( CPanel, SwitchEntity )
CPanel:AddControl( “Header”, { Text = “#Tool_bodygroup_name”,
Description = “#Tool_bodygroup_desc
} )

if ValidEntity(SwitchEntity) then
local maxbodygroups = SwitchEntity:BodyGroupCount()
if maxbodygroups > 1 then
CPanel:AddControl(“Slider”, { Label = “Select body group”,
Description = “Number of body groups the model has.”,
Type = “Integer”,
Min = 0,
Max = maxbodygroups-1,
Command = “bodygroupswitcher_bodygroup”
} )
else
CPanel:AddControl(“Label”, { Text = “This model only has one body group.” } )
end
else
CPanel:AddControl(“Label”, { Text = “No model selected.” } )
end
end

function TOOL:RebuildControlPanel()
local CPanel = GetControlPanel( “bodygroupswitcher” )
if ( !CPanel ) then return end
CPanel:ClearControls()
self.BuildCPanel(CPanel, self:GetBodyGroupSwitcherEntity())
end

function TOOL:DrawHUD()

local selected = self:GetBodyGroupSwitcherEntity()

if ( !ValidEntity( selected ) ) then return end

	local scrpos = selected:GetPos():ToScreen()
	if (!scrpos.visible) then return end
	
	local player_eyes = LocalPlayer():EyeAngles()
	local side = (selected:GetPos() + player_eyes:Right() * 50):ToScreen()
	local size = math.abs( side.x - scrpos.x )
	
	surface.SetDrawColor( 255, 255, 255, 255 )
	surface.SetTexture(surface.GetTextureID( "gui/faceposer_indicator"))
	surface.DrawTexturedRect( scrpos.x-size, scrpos.y-size, size*2, size*2 )

end

end

function TOOL:GetBodyGroupSwitcherEntity()
return self:GetWeapon():GetNetworkedEntity( 1 )
end

function TOOL:SetBodyGroupSwitcherEntity( ent )
return self:GetWeapon():SetNetworkedEntity( 1, ent )
end

function TOOL:LeftClick(Trace)

if not Trace.Entity:IsValid() then
return false
end

if ( CLIENT ) then return true end

local bodygroups = Trace.Entity:BodyGroupCount()

if bodygroups <= 1 then
return false
else
local currentbodygroup = Trace.Entity:GetBodyGroup()
local newbodygroup = 0
if (currentbodygroup + 1) >= bodygroups then
newbodygroup = currentbodygroup + 1 - bodygroups
else
newbodygroup = currentbodygroup+1
end
Trace.Entity:SetBodyGroup(newbodygroup)
end

return true

end

function TOOL:Reload(Trace)

if not Trace.Entity:IsValid() then
return false
end

if ( CLIENT ) then return true end

local bodygroups = Trace.Entity:BodyGroupCount()

if bodygroups == 1 then
return false
else
local currentbodygroup = Trace.Entity:GetBodyGroup()
local newsbodygroup = currentbodygroup
while newbodygroup == currentbodygroup do
newbodygroup = math.random(bodygroups)
end
Trace.Entity:SetBodyGroup(newbodygroup)
end

return true

end

function TOOL:RightClick(Trace)
if Trace.Entity:IsValid() then
if ( CLIENT ) then return true end
self.SelectedEntity = Trace.Entity
self:SetBodyGroupSwitcherEntity(self.SelectedEntity)
else
self.SelectedEntity = nil
return false
end
end

function TOOL:Think()
if ( CLIENT ) then
if not (BodyGroupswitcherPreviousEntity == self:GetBodyGroupSwitcherEntity()) then
self:RebuildControlPanel()
BodyGroupswitcherPreviousEntity = self:GetBodyGroupSwitcherEntity()
end
return
end

newslider = self:GetClientNumber(“bodygroup”)
if newslider ~= oldslider then
oldslider = newslider
manualcontrol = false
else
manualcontrol = true
end

if self.SelectedEntity then
if self.SelectedEntity:IsValid() then
if self.SelectedEntity:BodyGroupCount() > 1 then
if not manualcontrol then
self.SelectedEntity:SetBodyGroup(newslider)
end
end
end
end
end
[/lua]

The errors I get:


weapons\gmod_tool\stools/bodygroupswitcher.lua:95: attempt to call method 'BodyGroupCount' (a nil value)

On clicking a ragdoll.


weapons\gmod_tool\stools/bodygroupswitcher.lua:169: attempt to call method 'BodyGroupCount' (a nil value)


weapons\gmod_tool\stools/bodygroupswitcher.lua:30: attempt to call method 'BodyGroupCount' (a nil value)

Both of these on selecting a ragdoll.

Can anyone help then?

BodyGroupCount is a nil value.

Yer it doesn’t exist.

[editline]03:43PM[/editline]

I’m not an expert on ragdolls but will this work?

Entity:GetPhysicsObjectCount( )

Description:
Returns the number of physics objects in this (if this is a ragdoll it will be greater then 1 )

[editline]03:44PM[/editline]

Also Entity:GetBodygroup( Integer Bodygroup Number ) needs an integer as its arguement. Read the wiki better!

How could I stop making it a nil value?

Your not telling us much, you need to tell us what tool your using. All your telling us is the lua errors.

If IT is a original garrysmod tool then you probly fucked with something OR the ragdoll model is incomplete in completion XD
:slight_smile:

incoplete in completion should be some sorta motto or catchpraise

It is an edit of the Skin Switcher tool, to switch model body groups instead. And I tested it on the TF2 ragdolls.

Lua errors are all we need to know - And, of course, the code.

And, you need to define the value of BodyGroupCount in your script somewhere.

Where though?

Well, where do you need it? Clientside!

I meant in the code.

Can anyone help with this then?

Where it’s inside the scope you need it.

What?

You are using the wrong function :

There is no way the get the amounts of bodygroups.