Ulysses

General => Developers Corner => Topic started by: Major_Pain on August 31, 2009, 03:17:30 PM

Title: Lua Help
Post by: Major_Pain on August 31, 2009, 03:17:30 PM
I am trying to make a script that gives guns to certain groups. I have been learning a little bit of lua from jay but I seem to be stumped on this one. Help is greatly appreciated.

Code: [Select]
local function StartingGuns( ply )
ply:StripWeapons()
ply:Give( "weapon_physcannon" )
ply:Give( "weapon_physgun" )
ply:Give( "gmod_tool" )
ply:Give( "gmod_camera" )
if ply:IsUserGroup("admin") then
ply:Give( "weapon_punisher" )
ply:Give( "ulx_gun" )
end
if ply:IsUserGroup("superadmin") then
ply:Give( "weapon_punisher" )
ply:Give( "ulx_gun" )
ply:Give( "weapon_ar2" )
end
if ply:IsUserGroup("owner") then
ply:Give( "weapon_punisher" )
ply:Give( "ulx_gun" )
ply:Give( "weapon_haxgun" )
ply:Give( "weapon_ar2")
end
end

hook.Add( "PlayerLoadout", "StartingGuns", StartingGuns)

local function FirstSpawn( ply )
if ply:isAdmin or ply:isSuperAdmin then
ply:PrintMessage( HUD_PRINTTALK, "Major Pain: Oh hai thar admin! Lolz" )
end
end

hood.Add( "PlayerInitialSpawn", "FirstSpawn", FirstSpawn)

local function Spawn
if ply:IsUserGroup("owner") then
ply:SetGravity( 0, 90 )
ply:SetWalkSpeed( 325 )
ply:SetRunSpeed( 1000 )
end
end

hook.Add( "PlayerSpawn", "PlayerSpawn", Spawn)

I put the "Oh hai thar admin" thing for testing purposes.
Title: Re: Lua Help
Post by: jay209015 on August 31, 2009, 03:18:57 PM
Code: [Select]
local function Spawnshould be:

Code: [Select]
local function Spawn()
Title: Re: Lua Help
Post by: Major_Pain on August 31, 2009, 03:23:04 PM
Lol, how silly of me. Thanks jay. I suppose I will leave this open for more lua questions in the future if that's alright. I'm only a beginner.  :)

EDIT: Tested and it still doesn't work.
Title: Re: Lua Help
Post by: jay209015 on August 31, 2009, 03:36:26 PM
Code: [Select]
if ply:isAdmin or ply:isSuperAdmin thenneeds to be:
Code: [Select]
if ply:IsAdmin() then
IsSuperadmin() is not needed, since they are a part of IsAdmin() already.
Title: Re: Lua Help
Post by: Major_Pain on August 31, 2009, 03:38:08 PM
Updated Code:
Code: [Select]
local function StartingGuns( ply )
ply:StripWeapons()
ply:Give( "weapon_physcannon" )
ply:Give( "weapon_physgun" )
ply:Give( "gmod_tool" )
ply:Give( "gmod_camera" )
if ply:IsUserGroup("admin") then
ply:Give( "weapon_punisher" )
ply:Give( "ulx_gun" )
end
if ply:IsUserGroup("superadmin") then
ply:Give( "weapon_punisher" )
ply:Give( "ulx_gun" )
ply:Give( "weapon_ar2" )
end
if ply:IsUserGroup("owner") then
ply:Give( "weapon_punisher" )
ply:Give( "ulx_gun" )
ply:Give( "weapon_haxgun" )
ply:Give( "weapon_ar2")
end
end

hook.Add( "PlayerLoadout", "StartingGuns", StartingGuns)

local function FirstSpawn( ply )
if ply:IsAdmin() then
ply:PrintMessage( HUD_PRINTTALK, "Major Pain: Oh hai thar admin! Lolz" )
end
end

hood.Add( "PlayerInitialSpawn", "FirstSpawn", FirstSpawn)

local function Spawn ( ply )
if ply:IsUserGroup("owner") then
ply:SetGravity( 0, 90 )
ply:SetWalkSpeed( 325 )
ply:SetRunSpeed( 1000 )
end
end

hook.Add( "PlayerSpawn", "PlayerSpawn", Spawn)

Testing now.

Edit: Still doesn't work and it says this in chat: "Welcome to 61100st! We're playing"
Title: Re: Lua Help
Post by: jay209015 on August 31, 2009, 03:47:21 PM
Line 32:
Code: [Select]
hood.Add( "PlayerInitialSpawn", "FirstSpawn", FirstSpawn)I think you know what that should be :P
Title: Re: Lua Help
Post by: Major_Pain on August 31, 2009, 03:52:23 PM
Line 32:
Code: [Select]
hood.Add( "PlayerInitialSpawn", "FirstSpawn", FirstSpawn)I think you know what that should be :P

Lol, fixed that and now the chat thing works but still doesn't strip my weapons and give me the new ones.

Updated Code:
Code: [Select]
local function StartingGuns( ply )
ply:StripWeapons()
ply:Give( "weapon_physcannon" )
ply:Give( "weapon_physgun" )
ply:Give( "gmod_tool" )
ply:Give( "gmod_camera" )
if ply:IsUserGroup("admin") then
ply:Give( "weapon_punisher" )
ply:Give( "ulx_gun" )
end
if ply:IsUserGroup("superadmin") then
ply:Give( "weapon_punisher" )
ply:Give( "ulx_gun" )
ply:Give( "weapon_ar2" )
end
if ply:IsUserGroup("owner") then
ply:Give( "weapon_punisher" )
ply:Give( "ulx_gun" )
ply:Give( "weapon_haxgun" )
ply:Give( "weapon_ar2")
end
end

hook.Add( "PlayerLoadout", "StartingGuns", StartingGuns)

local function FirstSpawn( ply )
if ply:IsAdmin() then
ply:PrintMessage( HUD_PRINTTALK, "Major Pain: Oh hai thar admin! Lolz" )
end
end

hook.Add( "PlayerInitialSpawn", "FirstSpawn", FirstSpawn)

local function Spawn ( ply )
if ply:IsUserGroup("owner") then
ply:SetGravity( 0, 90 )
ply:SetWalkSpeed( 325 )
ply:SetRunSpeed( 1000 )
end
end

hook.Add( "PlayerSpawn", "PlayerSpawn", Spawn)

EDIT: Tried to change PlayerLoadout to PlayerSpawn on the "StartingGuns" function and that didn't work either.
Title: Re: Lua Help
Post by: Major_Pain on August 31, 2009, 04:43:58 PM
Ok! So after a lot of fail on my part the wiki prevailed.

Now I have a new problem.
Thanks Jay!
Title: Re: Lua Help
Post by: JamminR on August 31, 2009, 06:13:40 PM
Using a dedicated server?
Those are server functions, so if you're only editing locally then joining your server, those functions won't run.

What errors are you getting, if they aren't working?
Title: Re: Lua Help
Post by: Major_Pain on August 31, 2009, 06:27:28 PM
Using a dedicated server?
Those are server functions, so if you're only editing locally then joining your server, those functions won't run.

What errors are you getting, if they aren't working?

I was testing on my local machine by creating a lan server. I am using it for a legit server now thanks to jay's help off of the forums.
Title: Re: Lua Help
Post by: Stickly Man! on August 31, 2009, 11:41:14 PM
Edit: Still doesn't work and it says this in chat: "Welcome to 61100st! We're playing"

Random question, are you using or have you tested XGUI at all? I noticed my listen server saying that a while ago, I just hope it wasn't something I did  :o

EDIT: Never mind, I saw your post in the XGUI thread.. do you remember exactly when it started saying this?
Title: Re: Lua Help
Post by: Major_Pain on September 01, 2009, 02:33:32 PM
Random question, are you using or have you tested XGUI at all? I noticed my listen server saying that a while ago, I just hope it wasn't something I did  :o

EDIT: Never mind, I saw your post in the XGUI thread.. do you remember exactly when it started saying this?

After I fixed a hook breaking.
Title: Re: Lua Help
Post by: Major_Pain on September 02, 2009, 08:50:04 PM
Okay! I have started a new script that sets prop limit per user group. All I need to know is how to figure out a person's prop count. I tried doing ply:GetCount( "props" ). I wasn't successful.
Title: Re: Lua Help
Post by: JamminR on September 03, 2009, 02:57:44 PM
Major Pain, see our UNoLimited script in releases.
Though not done by usergroup other than admin and superadmin (yet), it should give you a good basis to look in for at least the counting portions.
Title: Re: Lua Help
Post by: Major_Pain on September 03, 2009, 03:04:20 PM
Jay informed me that GetCount("props") is correct, so my script must be wrong somewhere. I am going to post it here:

Code: [Select]
local totalProps = {}

local function PropLimit( ply )
        totalProps[ply] = ply:GetCount("props")
if ply:IsUserGroup("minge") then
return false
end
if ply:IsUserGroup("user") then
if totalProps[ply]>30 then
return false
end
end
if ply:IsUserGroup("starter") then
if totalProps[ply]>50 then
return false
end
end
if ply:IsUserGroup("regular") then
if totalProps[ply]>70 then
return false
end
end
if ply:IsUserGroup("members") then
if totalProps[ply]>100 then
return false
end
end
if ply:IsUserGroup("gettingthere") then
if totalProps[ply]>125 then
return false
end
end
if ply:IsUserGroup("there") then
if totalProps[ply]>150 then
return false
end
end
if ply:IsUserGroup("cavedweller") then
if totalProps[ply]>175 then
return false
end
end
if ply:IsUserGroup("nolife") then
if totalProps[ply]>200 then
return false
end
end
if ply:IsUserGroup("moderator") then
if totalProps[ply]>300 then
return false
end
end
end

hook.Add( "PlayerSpawnProp", "blockProps", PropLimit)

local function CantOpen ( ply )
if ply:IsUserGroup("minge") then
ply:PrintMessage( HUD_PRINTTALK, "Silly minge! Props are for players!" )
return false
end
end

hook.Add( "OnSpawnMenuOpen", "OpenMenu", CantOpen)

I'll go take a look at the UNoLimited.
Title: Re: Lua Help
Post by: Major_Pain on September 03, 2009, 04:32:00 PM
Ok! After a lot of testing and a lot of help from jay, I got the script finished. The spawn menu thing is a clientside hook so I took it out of the server script and made a clientside script for that.

Server:
Code: [Select]
AddCSLuaFile("autorun/client/MingeSpawnMenu.lua")

local totalProps = {}

local function PropLimit( ply )
totalProps[ply] = ply:GetCount("props")
if ply:IsUserGroup("minge") then
ply:PrintMessage( HUD_PRINTTALK, "You have hit your prop limit of 0!" )
return false
end
if ply:IsUserGroup("user") then
if totalProps[ply]>=30 then
ply:PrintMessage( HUD_PRINTTALK, "You have hit your prop limit of 30!" )
return false
end
end
if ply:IsUserGroup("starter") then
if totalProps[ply]>=50 then
ply:PrintMessage( HUD_PRINTTALK, "You have hit your prop limit of 50!" )
return false
end
end
if ply:IsUserGroup("regular") then
if totalProps[ply]>=70 then
ply:PrintMessage( HUD_PRINTTALK, "You have hit your prop limit of 70!" )
return false
end
end
if ply:IsUserGroup("members") then
if totalProps[ply]>=100 then
ply:PrintMessage( HUD_PRINTTALK, "You have hit your prop limit of 100!" )
return false
end
end
if ply:IsUserGroup("gettingthere") then
if totalProps[ply]>=125 then
ply:PrintMessage( HUD_PRINTTALK, "You have hit your prop limit of 125!" )
return false
end
end
if ply:IsUserGroup("there") then
if totalProps[ply]>=150 then
ply:PrintMessage( HUD_PRINTTALK, "You have hit your prop limit of 150!" )
return false
end
end
if ply:IsUserGroup("cavedweller") then
if totalProps[ply]>=175 then
ply:PrintMessage( HUD_PRINTTALK, "You have hit your prop limit of 175!" )
return false
end
end
if ply:IsUserGroup("nolife") then
if totalProps[ply]>=200 then
ply:PrintMessage( HUD_PRINTTALK, "You have hit your prop limit of 200!" )
return false
end
end
if ply:IsUserGroup("moderator") then
if totalProps[ply]>=300 then
ply:PrintMessage( HUD_PRINTTALK, "You have hit your prop limit of 300!" )
return false
end
end
end

hook.Add( "PlayerSpawnProp", "blockProps", PropLimit)

Client:
Code: [Select]
AddCSLuaFile("autorun/client/MingeSpawnMenu.lua")

local function CantOpen ()
if LocalPlayer():IsUserGroup("minge") then
LocalPlayer():PrintMessage( HUD_PRINTTALK, "Silly minge! Props are for players!" )
return false
end
end

hook.Add( "OnSpawnMenuOpen", "OpenMenu", CantOpen)
Title: Re: Lua Help
Post by: JamminR on September 03, 2009, 08:49:34 PM
Your 'order' of group tests may be contributing to problems.
Using the normal release version of ULib, anyone in a group other than user is ALSO a 'user'.
Then, depending on how you have inheritence set up, if you do, other groups may be included in ones higher, if you inherited them.
If you're using the latest SVNs...that may not be true anymore.
So, example
If regular inherits smarter inherits user inherits minge.
The following are true;
1) All 4 groups are 'user'
2) IsUserGroup minge will be true for smarter
3) IsUsergroup smarterwill be true for smarter, regular and minge
.... hopefully you get the point.
This is one reason why we're changing the ULib so that true won't always be returned.
Title: Re: Lua Help
Post by: jay209015 on September 04, 2009, 05:08:11 AM
He's running the latest svn version :D
Title: Re: Lua Help
Post by: Major_Pain on September 11, 2009, 06:25:04 PM
Ok! If you have read the AutoPromote thread, I promised I would remake it. I have been struggling with this thing for a week now.

Here is my code:
Code: [Select]
--Format is { ["hours"] = "group" }
--If you aren't using a group, put nil!! Ex: "A9 = nil"
A1 = { ["1"] = "starter" }
A2 = { ["2"] = "regular" }
A3 = { ["3"] = "members" }
A4 = { ["4"] = "gettingthere" }
A5 = { ["5"] = "there" }
A6 = { ["6"] = "cavedweller" }
A7 = { ["7"] = "nolife" }
A8 = nil
A9 = nil
A10 = nil

--Exclusion groups go here
B1 = "minge"
B2 = "owner"
B3 = "admin"
B4 = "superadmin"
B5 = "moderator"
B6 = nil
B7 = nil
B8 = nil
B9 = nil
B10 = nil

---Don't edit below this line unless you know what you are doing!---

function excludedCheck( ply )
local exclude = false
if ply:IsUserGroup(B1) or ply:IsUserGroup(B2) or ply:IsUserGroup(B3) or ply:IsUserGroup(B4) or ply:IsUserGroup(B5) or ply:IsUserGroup(B6) or ply:IsUserGroup(B7) or ply:IsUserGroup(B8) or ply:IsUserGroup(B9) or ply:IsUserGroup(B10) then
exclude = true
end
return exclude
end

function groupSet( ply )
local hours = math.floor((ply:GetUTime() + CurTime() - ply:GetUTimeStart())/60/60)
if A10 then
for k1,v1 in pairs(A10) do
if hours>=k1 then groupAP = v1
return true
end
end
end
if A9 then
for k2,v2 in pairs(A9) do
if hours>=k2 then groupAP = v2
return true
end
end
end
if A8 then
for k3,v3 in pairs(A8) do
if hours>=k3 then groupAP = v3
return true
end
end
end
if A7 then
for k4,v4 in pairs(A7) do
if hours>=k4 then groupAP = v4
return true
end
end
end
if A6 then
for k5,v5 in pairs(A6) do
if hours>=k5 then groupAP = v5
return true
end
end
end
if A5 then
for k6,v6 in pairs(A5) do
if hours>=k6 then groupAP = v6
return true
end
end
end
if A4 then
for k7,v7 in pairs(A4) do
if hours>=k7 then groupAP = v7
return true
end
end
end
if A3 then
for k8,v8 in pairs(A3) do
if hours>=k8 then groupAP = v8
return true
end
end
end
if A2 then
for k9,v9 in pairs(A2) do
if hours>=k9 then groupAP = v9
return true
end
end
end
if A1 then
for k10,v10 in pairs(A1) do
if hours>=k10 then groupAP = v10
return true
end
end
end
return false
end

function promotePlayer( ply )
if not excludedCheck( ply ) then
if groupSet( ply ) then
game.ConsoleCommand("ulx adduser "..string.format("%q", ply:Nick() ).." "..string.format("%q", groupAP).." \n")
end
end
end

hook.Add( "PlayerDeath", "onDeath", promotePlayer)

I am going to convert the exclude groups and stuff at the top into tables but I wanted to get it working first. I have rewrote this code at least 3-4 times. When I go to test it, I kill my self and it gives me an error in the server console:
Code: [Select]
ERROR: Hook 'onDeath' Failed: autorun/server/AutoPromote2_2.lua:61: attempt to compare string with number
Removing Hook 'onDeath'

This is line 61:
Code: [Select]
if hours>=k4 then groupAP = v4
I can't figure this out. Help please.

EDIT: I figured it out right after. The quotes in the A1-A10 are making the number a string. I changed it to just A1=[1] instead of A1=["1"] and now it works perfectly! I am going to go post this in the auto promote thread.