• SWEP to choose a function at random on SWEP:PrimaryAttack()
    2 replies, posted
Hi, I'm trying to develop a SWEP that can call upon 8 different functions but each time the user uses the primary attack it randomly chooses which one to call. I can't figure out how to make it randomly choose, and the current code I have makes the SWEP not show in the weapons folder. CODE: AddCSLuaFile() SWEP.PrintName = "sweptest" SWEP.Author = "yaboihuskii" SWEP.Instructions = "random SWEP 8 chooser bang wallop yeet" SWEP.Spawnable= true SWEP.AdminSpawnable= true SWEP.AdminOnly = false SWEP.ViewModelFOV = 70 SWEP.ViewModel = "models/weapons/v_pistol.mdl"  SWEP.WorldModel = "models/weapons/w_pistol.mdl" SWEP.ViewModelFlip = false SWEP.AutoSwitchTo = false SWEP.AutoSwitchFrom = false SWEP.Slot = 1 SWEP.SlotPos = 1 SWEP.UseHands = false SWEP.HoldType = "Pistol"  SWEP.FiresUnderwater = false SWEP.DrawCrosshair = false SWEP.DrawAmmo = false SWEP.ReloadSound = "" SWEP.Base = "weapon_base" SWEP.Primary.TakeAmmo = 1 SWEP.Primary.ClipSize = 250 SWEP.Primary.Ammo = "Pistol" SWEP.Primary.DefaultClip = 8 SWEP.Primary.NumberofShots = 1 SWEP.Primary.Automatic = false SWEP.Primary.Delay = 2 SWEP.Primary.Force = 25 SWEP.Secondary.ClipSize = 0 SWEP.Secondary.DefaultClip = 0 SWEP.Secondary.Automatic = false SWEP.Secondary.Ammo = "none" SWEP.CSMuzzleFlashes = false function SWEP:PrimaryAttack() swepchoice() end function swepchoice() local rndchoice = math.Rand(1, 8) if rndchoice = 1 then soundatt() elseif rndchoice = 2 then propspawn() elseif rndchoice = 3 then serverprint() elseif rndchoice = 4 then clientprint() elseif rndchoice = 5 then colour() elseif rndchoice = 6 then ignite() elseif rndchoice = 7 then gravityset() elseif rndchoice = 8 then givehp() end end function soundatt() sound.Play( "ambient/explosions/exp1.wav", Vector( 0, 0, 0 ) ) file.Write("sweptest.txt", Entity(1):SteamID() .. " " .. Entity(1):Nick() .. " Randomly produced sound via SWEP") end function propspawn() barrel=ents.Create("prop_physics") barrel:SetModel("models/props_c17/oildrum001.mdl") barrel:SetPos(Vector(0,0,0)) barrel:Spawn() file.Write("sweptest.txt", Entity(1):SteamID() .. " " .. Entity(1):Nick() ..  " Randomly spawned prop via SWEP") end function serverprint() print("I have printed to the server via a SWEP") file.Write("sweptest.txt", Entity(1):SteamID() .. " " .. Entity(1):Nick() ..  " Random server print via SWEP") end function clientprint() Entity(1):PrintMessage( HUD_PRINTTALK, "I have printed to HUD via a SWEP") file.Write("sweptest.txt", Entity(1):SteamID() .. " " .. Entity(1):Nick() ..  " Random client print via SWEP") end function colour() Entity(1):ScreenFade( SCREENFADE.IN, Color( 0, 0, 255, 128 ), 2.0, 0 ) file.Write("sweptest.txt", Entity(1):SteamID() .. " " .. Entity(1):Nick() ..  " Randomly produced colour via SWEP") end function ignite() Entity(1):Ignite(5) file.Write("sweptest.txt", Entity(1):SteamID() .. " " .. Entity(1):Nick() ..  " Randomly lit on fire via SWEP") end function gravityset() RunConsoleCommand( "sv_gravity" , "250") file.Write("sweptest.txt", Entity(1):SteamID() .. " " .. Entity(1):Nick() ..  " Randomly changed gravity via SWEP") end function givehp() Entity( 1 ):SetHealth( Entity( 1 ):Health() + 50 ) file.Write("sweptest.txt", Entity(1):SteamID() .. " " .. Entity(1):Nick() ..  " Randomly gave HP via SWEP") end
Change your = to == in your elseif block. though if you wanted to improve the code I’d recommend using a table and table.Random
Yeah I've actually just figured that out before you put your comment, but thanks though! Also yeah the code could definitely be improved, which I'll probably end up doing either at a later date or on different projects; still in the early learning phases at the moment.
Sorry, you need to Log In to post a reply to this thread.