I'm trying to toy around with the sun entity "env_sun" and for some odd reason nothing works for me when I try to modify except for ent:Remove(). Key Values don't do anything neither do typical entity functions. Here's my code:
[lua]
concommand.Add("env_suntest",function(player,_,args)
ents.FindByClass("env_sun")[1]:SetKeyValue("Pitch",tonumber(args[1]))
end)
[/lua]
This is just to toy around so I can figure out how to work with it.
[editline]16th August 2012[/editline]
[QUOTE=Coffeee;37271991][lua]
tsql = tmysql.initialize(ad, uz, aa, db, 3306)
-- Then later in the file:
tsql:query(queryz, query_correct, 1, funcz or function() end) -- attempt to call field 'query' (a nil value)
[/lua]
Still gives me the same error. This is what you meant right?[/QUOTE]
It's tsql:Query(), not query() I believe.
[QUOTE=jrj996;37273099]
It's tsql:Query(), not query() I believe.[/QUOTE]
Thanks alot. I have other errors now but atleast I'm not completely stuck.
[QUOTE=Mythikos;37259742]Hey guys, sorry to ask so many questions lol (im still learning) and this thread is awesome because there isn't much help anywhere else :). Im trying to make a command for the gamemode gofish2. The command would allow me to give other players money rather than just myself.
.
This is what I have so far.
[lua]
concommand.Add("gfc_givecash", function(ply, args)
if ply:IsAdmin() then
if args[2] then
local p = args[1]
local num = args[2]
p:SetNetworkedInt("cash", p:GetNetworkedInt("cash") + num)
umsg.Start("MoneyChanged", p)
umsg.Short(p:GetNetworkedInt("cash"))
umsg.End()
else
Msg("Insufficient amount of arguments! gfc_givecash 'name' 'amount'")
end
//end
end)
[/lua]
and This is what the old lua code is.
[lua]
function givecash(ply)
if ply:IsAdmin() then
ply:SetNetworkedInt("cash", ply:GetNetworkedInt("cash") + 500)
umsg.Start("MoneyChanged",ply)
umsg.Short(ply:GetNetworkedInt("cash"))
umsg.End()
end
end
concommand.Add("givecash", givecash)
[/lua]
The error the console is spitting back at me is Lua Error: [@gamemodes\gofish2\gamemode\init.lua:40] bad key to string index (number expected, got string) and note this is happening to my script, not the original.
I am not sure on how to fix this one, or even if im doing it right at that matter. If anyone knows a way/has a better way please tell :). Btw Line 40 is the "p:SetNetworkedInt("cash", p:GetNetworkedInt("cash") + num)" part of the code. Thanks!
[/QUOTE]
Can anyone help me please?
Does anyone know how I can find out if an entity was created by world?
[QUOTE=Mythikos;37276804]Can anyone help me please?[/QUOTE]
The concommand callback's arguments are player, command, arguments. You're attempting to treat a string as an array.
[editline]17th August 2012[/editline]
[QUOTE=Ronon Dex;37276826]Does anyone know how I can find out if an entity was created by world?[/QUOTE]
Entity:CreatedByMap()
[editline]17th August 2012[/editline]
[QUOTE=Lexic;37276852]The concommand callback's arguments are player, command, arguments. You're attempting to treat a string as an array.[/QUOTE]
Actually, while that is an issue it's not what's causing the error.
[lua]local p = args[1]
local num = args[2][/lua]
args is an array of strings, so both p and num are strings. You need to call tonumber() on num and perform some form of lookup to get the player object.
[QUOTE=Banana Lord.;37269284]have you tried with net.Send( { ply } )[/QUOTE]
Yes, and I'm getting the same result.
[QUOTE=Lexic;37276852]The concommand callback's arguments are player, command, arguments. You're attempting to treat a string as an array.[/QUOTE]
I understand but I am not sure how to remedy an issue like this. Possibly can you help further. possibly like an example or something. thanks and sorry :\
Also side note: I am trying to figure out how to set a prop to owner world... I'm using SPP on my server and need to be able to set certain entities to "Owner: World" so other players cant interact with them. I tried the "Entity:SetOwner()" function but it isn't doing what I need. Any help on this would be greatly appreciated!
Not sure if the issue is in my code or if Garry messed up something in the update but whenever I die my player's ragdoll entity doesn't "spawn". It does a weird camera glitch where it levitates over where my dead body SHOULD be but there's nothing there. Here's the only hooks that could interfere with it that I have:
[lua]
hook.Add("PlayerDeath","Check Match Status",function(victim,inf,pl)
if GAMEMODE:GetAlivePlayers() - 1 == 1 then
if victim != player then
GAMEMODE:EndMatch(pl)
else
--Still working on it
end
end
print("Ran hook.")
end)
hook.Add("PlayerDeathThink","Prevent Match Spawning",function(player)
if GAMEMODE.MatchStarted then
return false
end
end)
[/lua]
Also, this ONLY happens when a bot is spawned. If I don't spawn it or I'm in single player then it works fine.
[QUOTE=Mythikos;37277763]I understand but I am not sure how to remedy an issue like this. Possibly can you help further. possibly like an example or something. thanks and sorry :\[/QUOTE]
You have put
[lua]concommand.Add("gfc_givecash", function(ply, args)[/lua]
change it to
[lua]concommand.Add("gfc_givecash", function(ply, cmd, args)[/lua]
See also:
[QUOTE=Lexic;37276852]Actually, while that is an issue it's not what's causing the error.
[lua]local p = args[1]
local num = args[2][/lua]
args is an array of strings, so both p and num are strings. You need to call tonumber() on num and perform some form of lookup to get the player object.[/QUOTE]
[QUOTE=Lexic;37282614]You have put
[lua]concommand.Add("gfc_givecash", function(ply, args)[/lua]
change it to
[lua]concommand.Add("gfc_givecash", function(ply, cmd, args)[/lua]
See also:[/QUOTE]
I am starting to get it. I just need help understanding one last thing. This is my new code...
[LUA]
concommand.Add("gfc_givecash", function(ply, cmd, args)
if ply:IsAdmin() then
if args[2] then
local p = args[1]
local num = args[2]
p:SetNetworkedInt("cash", p:GetNetworkedInt("cash", 0) + tonumber(num))
umsg.Start("MoneyChanged", p)
umsg.Short(p:GetNetworkedInt("cash"))
umsg.End()
else
Msg("Incorrect number of arguments! gfc_givecash 'name' 'amount'")
end
end
end)
[/lua]
And the error is still "init.lua:40] bad key to string index (number expected, got string)". I am assuming it has to do with what you said about checking for the player, but how exactly would I do that?
Thank again for any help in advanced. Its nice to have such a good resource on facepunch.com
Pop this somewhere that your script will be able to find it (say at the top of the script) and then use local p = player.Get(args[1])
[lua]---
-- Gets a player by a part of their name, or their steamID, or their UniqueID, or their UserID.
-- Will provide the player with the shortest name that matches the key. That way a search for 'lex' will return 'Lexi' even if 'LeXiCaL1ty{GT}' is available.
-- @param id An ID to search for the player by.
-- @return A player if one is found, nil otherwise.
-- @author Lexi R
function player.Get(id)
local res, len, name, num, pname, lon;
name = string.lower(id);
id = string.upper(id);
num = tonumber(id);
for _,ply in pairs(player.GetAll()) do
pname = ply:Name():lower();
if ((num and (ply:UniqueID() == num or ply:UserID() == num)) or ply:SteamID() == id) then
return ply;
elseif (pname:find(name)) then
lon = pname:len();
if (res) then
if (lon < len) then
res = ply;
len = lon;
end
else
res = ply;
len = lon;
end
end
end
return res;
end[/lua]
Incidentally, you're not checking to see if the input arguments are valid. Try something like this
[lua]local p = player.Get(args[1])
local num = tonumber(args[2])
if not p then
ply:ChatPrint("Invalid player '" .. args[1] .. "'!")
return -- End the function early
elseif not num then
ply:ChatPrint("Invalid amount '" .. args[2] .. "'!")
return
end[/lua]
[lua]
local p = args[1]
[/lua]
You are setting p equal to a string/number, then running entity only functions on it.
[QUOTE=Lexic;37282867]Pop this somewhere that your script will be able to find it (say at the top of the script) and then use local p = player.Get(args[1])
[lua]---
-- Gets a player by a part of their name, or their steamID, or their UniqueID, or their UserID.
-- Will provide the player with the shortest name that matches the key. That way a search for 'lex' will return 'Lexi' even if 'LeXiCaL1ty{GT}' is available.
-- @param id An ID to search for the player by.
-- @return A player if one is found, nil otherwise.
-- @author Lexi R
function player.Get(id)
local res, len, name, num, pname, lon;
name = string.lower(id);
id = string.upper(id);
num = tonumber(id);
for _,ply in pairs(player.GetAll()) do
pname = ply:Name():lower();
if ((num and (ply:UniqueID() == num or ply:UserID() == num)) or ply:SteamID() == id) then
return ply;
elseif (pname:find(name)) then
lon = pname:len();
if (res) then
if (lon < len) then
res = ply;
len = lon;
end
else
res = ply;
len = lon;
end
end
end
return res;
end[/lua]
Incidentally, you're not checking to see if the input arguments are valid. Try something like this
[lua]local p = player.Get(args[1])
local num = tonumber(args[2])
if not p then
ply:ChatPrint("Invalid player '" .. args[1] .. "'!")
return -- End the function early
elseif not num then
ply:ChatPrint("Invalid amount '" .. args[2] .. "'!")
return
end[/lua][/QUOTE]
O... M... G... I cant thank you enough for your help. After reviewing the code snips, I see what lua was saying... Yet again it is so nice to have such a good resource here on facepunch.com. Thanks everyone.
So in the last 3 hours, i've been doing everything to resolve this issue, tho i cannot seem to find a way to fix this.
Heres my code:
[code]
local SW,SH = ScrW(),ScrH()
local HealthText = "Healthy"
local HealthColor = Color(0,102,0,255)
-- Health
surface.SetFont("HUDText_Health_Scaled")
surface.SetTextColor(HealthColor)
local x,y = surface.GetTextSize(HealthText)
surface.SetTextPos(ScreenScale(25), SH-ScreenScale(12.5)-y*0.5)
surface.DrawText(HealthText)
[/code]
Heres the error that repetitively gets spammed:
[gamemodes/testgm\gamemode\client\hud.lua:375] attempt to perform arithmetic on local 'y' (a nil value)(Hook: HUDPaint)
1. lua/includes/modules/hook.lua:106 (unknown)
Line 375 is "surface.SetTextPos(ScreenScale(25), SH-ScreenScale(12.5)-y*0.5)".
Please notice that this is from GMod 13.
Thank you in advance.
When in doubt, put a print statement. Try shoving print(x, y) before the line which errors and see what it outputs.
What happened to Vector:Normalize(Vector) in GMod 13? It no longer works properly for me now.
Use before:
[lua]
local t = p:GetEyeTrace();
local cv = self.Entity:WorldToLocal(t.HitPos):Normalize();
local da = math.deg(math.acos(v:DotProduct(cv)/v:Length()));
[/lua]
It says it can't DotProduct cv because it equals nil where as it should be a vector.
EDIT:
So I have to just do
[lua]
local cv = self.Entity:WorldToLocal(t.HitPos)
cv:Normalize()
[/lua]
Now then?
I've been trying to use the player classes in gmod 13 and on the wiki it says the following:
[QUOTE][CODE]DEFINE_BASECLASS( "player_default" )
local PLAYER = {}
--
-- See gamemodes/base/player_class/player_default.lua for all overridable variables
--
PLAYER.WalkSpeed = 200
PLAYER.RunSpeed = 400
function PLAYER:Loadout()
self.Player:RemoveAllAmmo()
self.Player:GiveAmmo( 256, "Pistol", true )
self.Player:Give( "weapon_pistol" )
end
player_manager.RegisterClass( "player_custom", PLAYER, "player_default" )[/CODE]
These player class functions are usually housed in gamemode/player_class/ - and they do not get loaded automatically. You should load it in your shared.lua gamemode file. And they should be loaded in the right order - so that base classes get loaded before their children. [/QUOTE]
What does it mean about loading them and/or How would i go about loading them?
Thanks in advance.
[QUOTE=Ronon Dex;37290670]What happened to Vector:Normalize(Vector) in GMod 13? It no longer works properly for me now.
Use before:
[lua]
local t = p:GetEyeTrace();
local cv = self.Entity:WorldToLocal(t.HitPos):Normalize();
local da = math.deg(math.acos(v:DotProduct(cv)/v:Length()));
[/lua]
It says it can't DotProduct cv because it equals nil where as it should be a vector.
EDIT:
So I have to just do
[lua]
local cv = self.Entity:WorldToLocal(t.HitPos)
cv:Normalize()
[/lua]
Now then?[/QUOTE]
It works the same way it always has. Normalize MODIFIES the vector, but doesn't return anything. GetNormalized/GetNormal return a vector, and don't modify the original.
[url]http://wiki.garrysmod.com/page/Classes/Vector/Normalize[/url]
[url]http://wiki.garrysmod.com/page/Classes/Vector/GetNormalized[/url]
[url]http://wiki.garrysmod.com/page/Classes/Vector/GetNormal[/url]
Is SpectateEntity also now broken after last nights update? Doesn't seem to work anymore for me.
I am working on a swep that will ragdoll a player when shot. Everything works perfectly except the ragdoll says "Owner: N/A" and can be claimed by another player. If the player then deletes that ragdolled player, the server crashes. I need to know how to set the ragdoll entity to owner: world by default or at least disable interaction with it.
This is the snip that dictates the ragdoll ness...
[lua]
local rag = ents.Create("prop_ragdoll")
rag:SetModel( plyTarget:GetModel() )
rag:SetPos( plyTargetPos )
rag:Spawn()
rag:Activate()
[/lua]
I am using SPP (Simple Prop Protection) on my server. Any help would be appreciated and thanks in advance :D
I think for setting owner this should work:
[lua]
rag:SetOwner(GetWorldEntity())
[/lua]
Hello, I'd like to know where I would put this to actually make it work, or if the code is just wrong, cause no one tells you where to put your HUD code after you make it...
[CODE]function HUDHide( hud )
for k, v in pairs{ "HUDHealth", "HUDBattery" }
if hud == v then
return false
end
end
end
hook.Add( "HUDShouldDraw", "HUDHide", HUDHide)
function GM:HUDPaint( )
self.BaseClass:HUDPaint( )
local person = LocalPlayer( )
local Health = LocalPlayer( ):Health( )
surface.CreateFont( "coolvetica",64,400,false,false,"tbsfont" )
surface.SetTextColor( 255,255,255,255 )
surface.SetTextPos( 34, (ScrH()/2)+(ScrH()/4) )
surface.SetFont( "tbsfont" )
surface.DrawText( "Health: "+Health )
end[/CODE]
[QUOTE=Xervion;37292467]Hello, I'd like to know where I would put this to actually make it work, or if the code is just wrong, cause no one tells you where to put your HUD code after you make it...
[CODE]function HUDHide( hud )
for k, v in pairs{ "HUDHealth", "HUDBattery" }
if hud == v then
return false
end
end
end
hook.Add( "HUDShouldDraw", "HUDHide", HUDHide)
function GM:HUDPaint( )
self.BaseClass:HUDPaint( )
local person = LocalPlayer( )
local Health = LocalPlayer( ):Health( )
surface.CreateFont( "coolvetica",64,400,false,false,"tbsfont" )
surface.SetTextColor( 255,255,255,255 )
surface.SetTextPos( 34, (ScrH()/2)+(ScrH()/4) )
surface.SetFont( "tbsfont" )
surface.DrawText( "Health: "+Health )
end[/CODE][/QUOTE]
In a clientside file such as cl_init.
[QUOTE=Coffeee;37292633]In a clientside file such as cl_init.[/QUOTE]
I know that, but where does the actual file go?
[QUOTE=Xervion;37292659]I know that, but where does the actual file go?[/QUOTE]
Either shove it into the lua/ folder in garrysmod/garrysmod and then run it or shove it into the lua/autorun/client folder in the same root folder and it will auto-run.
[QUOTE=Mythikos;37291843]I am working on a swep that will ragdoll a player when shot. Everything works perfectly except the ragdoll says "Owner: N/A" and can be claimed by another player. If the player then deletes that ragdolled player, the server crashes. I need to know how to set the ragdoll entity to owner: world by default or at least disable interaction with it.
This is the snip that dictates the ragdoll ness...
-snip-
I am using SPP (Simple Prop Protection) on my server. Any help would be appreciated and thanks in advance :D[/QUOTE]
[QUOTE=Ronon Dex;37291892]I think for setting owner this should work:
[lua]
rag:SetOwner(GetWorldEntity())
[/lua][/QUOTE]
This didn't work. The prop still shows up as Owner: N/A. This is my current code.
[lua]
local rag = ents.Create("prop_ragdoll")
rag:SetModel( plyTarget:GetModel() )
rag:SetOwner(GetWorldEntity())
rag:SetPos( plyTargetPos )
rag:Spawn()
rag:Activate()
[/lua]
Any other suggestions :O?
[QUOTE=wizardsbane;37293106]Either shove it into the lua/ folder in garrysmod/garrysmod and then run it or shove it into the lua/autorun/client folder in the same root folder and it will auto-run.[/QUOTE]
I tried that, and here is what I get when I try to run it with "lua_run_cl":
[CODE] lua_run_cl lua/hud.lua
[LuaCmd:1] '=' expected near '/'
[/CODE]
And when I just try to run it with lua_run, it gives me nothing, along with making it autorun.
Is there an easy way to show a visible line ingame to debug traces? [url=http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index332a.html]render.DrawBeam[/url] seems like the way to go, but I can't get it to show anything (working within the primary attack function of a SWEP).
-snip silly me(No it's not a fix.)-
Sorry, you need to Log In to post a reply to this thread.