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?