I tried to make a twitch feature for my STool.
What I mean with "twitch"? Adding a random ammount force to an entity. Minimum and maximum values are set by Sliders.
To get those numbers, i used [B][URL="http://wiki.garrysmod.com/?title=TOOL.GetClientNumber"]TOOL.GetClientNumber [IMG]http://wiki.garrysmod.com/favicon.ico[/IMG][/url][/B]
My code looks like this rith now:
[lua]TOOL.Category = "Render"
TOOL.Name = "#Tesla"
TOOL.Command = nil
TOOL.ConfigName = nil
TOOL.ClientConVar["twitch"] = 0
TOOL.ClientConVar["mintwitchforce"] = -3500
TOOL.ClientConVar["maxtwitchforce"] = 3500
function TOOL.Think()
local min = self:GetClientNumber("mintwitchforce")
local max = self:GetClientNumber("maxtwitchforce")
end
if (CLIENT) then
language.Add("Tool_tesla_name","Tesla")
language.Add("Tool_tesla_desc","ITS ALIIIVE!")
language.Add("Tool_tesla_0","shoot something asshole")
end
function TOOL.BuildCPanel(CPanel)
CPanel:AddControl("Header",{
Text = "#Tool_tesla_name",
Description = "#Tool_tesla_desc"})
CPanel:AddControl("CheckBox",{
Label = "Twitch",
Description = "Should the entity twitch?",
Command = "tesla_twitch"})
CPanel:AddControl("Slider",{
Label = "Minimum twitch force",
Type = "Float",
Min = -10000,
Max = 10000,
Command = "tesla_mintwitchforce"})
CPanel:AddControl("Slider",{
Label = "Maximum twitch force",
Type = "Float",
Min = -10000,
Max = 10000,
Command = "tesla_maxtwitchforce"})
end
function TOOL:LeftClick(Trace)
if not Trace.Entity:IsValid() then
return false
end
if (CLIENT) then return true end
local twitch = self:GetClientNumber("twitch")
local ent = Trace.Entity
Effect(ent)
if twitch == 1 then
Twitch(ent)
end
end
function Twitch(ent)
if twitch == 0 then return end
local TwitchX = math.random(min,max)
local TwitchY = math.random(min,max)
local TwitchZ = math.random(min,max)
ent:GetPhysicsObject():ApplyForceCenter(Vector(TwitchX,TwitchY,TwitchZ))
end
function Effect(ent)
local effect = EffectData()
effect:SetOrigin(ent:GetPos())
effect:SetStart(ent:GetPos())
effect:SetMagnitude(10)
effect:SetEntity(ent)
util.Effect("teslaHitBoxes",effect)
ent:EmitSound("Weapon_StunStick.Activate")
end[/lua]
whenever i click a prop i get this error:
[CODE][gamemodes\sandbox\entities\weapons\gmod_tool\stool.lua:77] attempt to concatenate local 'property' (a nil value)[/CODE]
i was liek zomg dats gerrys coed wtf did i broek geemawd
how do i get it to work?
[editline]21st July 2011[/editline]
when i remove the GetClientNumber's in the TOOL:LeftClick function and click something i get this error:
[code][addons\tesla\lua\weapons\gmod_tool\stools\tesla.lua:60] attempt to index global 'self' (a nil value)[/code]
The problem here is that you're forgetting to use strings in [I]self:GetClientNumber()[/I]. I changed that and tested it, and seems to work. However, i'd suggest using the object's mass as a factor when applying force. An object with a large mass will not be affected as much as one with a smaller mass.
Doesn't work.
I still get the [addons\tesla\lua\weapons\gmod_tool\stools\tesla.lua:60] attempt to index global 'self' (a nil value) error
[editline]21st July 2011[/editline]
updated code.
Oh yeah, the best way to do it is to specify the [I]Twitch()[/I] function as [I]TOOL:Twitch()[/I], this will make the [I]self[/I] variable available. You then need to call the function from within the [I]LeftClick()[/I] function using [I]self:Twitch()[/I]. Like this:
[lua]
TOOL.Category = "Render"
TOOL.Name = "#Tesla"
TOOL.Command = nil
TOOL.ConfigName = nil
TOOL.ClientConVar["twitch"] = 0
TOOL.ClientConVar["mintwitchforce"] = -3500
TOOL.ClientConVar["maxtwitchforce"] = 3500
if (CLIENT) then
language.Add("Tool_tesla_name","Tesla")
language.Add("Tool_tesla_desc","ITS ALIIIVE!")
language.Add("Tool_tesla_0","shoot something asshole")
end
function TOOL.BuildCPanel(CPanel)
CPanel:AddControl("Header",{
Text = "#Tool_tesla_name",
Description = "#Tool_tesla_desc"})
CPanel:AddControl("CheckBox",{
Label = "Twitch",
Description = "Should the entity twitch?",
Command = "tesla_twitch"})
CPanel:AddControl("Slider",{
Label = "Minimum twitch force",
Type = "Float",
Min = -10000,
Max = 10000,
Command = "tesla_mintwitchforce"})
CPanel:AddControl("Slider",{
Label = "Maximum twitch force",
Type = "Float",
Min = -10000,
Max = 10000,
Command = "tesla_maxtwitchforce"})
end
function TOOL:LeftClick(Trace)
if not Trace.Entity:IsValid() then
return false
end
if (CLIENT) then return true end
local twitch = self:GetClientNumber("twitch")
local ent = Trace.Entity
Effect(ent)
if twitch == 1 then
self:Twitch(ent)
end
end
function TOOL:Twitch(ent)
local min = self:GetClientNumber("mintwitchforce")
local max = self:GetClientNumber("maxtwitchforce")
if twitch == 0 then return end
local TwitchX = math.random(min,max)
local TwitchY = math.random(min,max)
local TwitchZ = math.random(min,max)
ent:GetPhysicsObject():ApplyForceCenter(Vector(TwitchX,TwitchY,TwitchZ))
end
function Effect(ent)
local effect = EffectData()
effect:SetOrigin(ent:GetPos())
effect:SetStart(ent:GetPos())
effect:SetMagnitude(10)
effect:SetEntity(ent)
util.Effect("teslaHitBoxes",effect)
ent:EmitSound("Weapon_StunStick.Activate")
end
[/lua]
it does nothing D:
no errors though
The code i posted works fine in my game, what conditions are you testing in? Have you restarted the game? Or used [B]lua_reloadent[/B]?
im on my laptop without any addons and dx level 8.
i tried everything you said.
all i get is the effect.
no force is added to the prop no matter whow high the sliders are set.
Have you made sure that the [B]Twitch[/B] checkbox is checked, and that the checkbox changes clientside ConVar?
yep
What prop are you testing with? If it's too heavy then your tool won't do much. I tried it with one of the HL2 barrels and it works fine. When the checkbox is checked, put [B]tesla_twitch[/B] into the console and it should have a value of 1. Other than that i cannot think of any reason why it wouldn't work apart from a problem with your setup. Are you testing in Single Player?
oh! it works! thanks!
No problem :) As i mentioned before, it's probably best to put some sort of automatic scaling in the script. The heavier the object, the more force that is applied.
Sorry, you need to Log In to post a reply to this thread.