table.Random is creating another number after it outputs itself

hello i have another issue im learning LUA now so bear with my noobness

i am making a weapon with random force value applied from a table,
my table is


	RNG = { 14,200 }

and what applies my force is


SWEP.ForceApply			= (table.Random(RNG))

but it keeps putting another number after it outputted

anyone knows how to work around this?

in the end the force applied should be 200, but 14 force will never ever get loaded

Unless I’m misreading, why not use math.random? (wait does that get a value between two numbers or pick one or the other)

fuck, ignore me

I don’t quite understand what exactly your problem is, but table.Random DOES output two values:

  1. A random VALUE from the table
  2. The KEY of the value

If you can’t solve it, post more of your code and maybe we can help out more.



RNG = math.random( 14,  200 )


That get’s a random number from 14 to 200. Pretty sure he wants it to randomly be 14 or 200 from what I see.

Try something like this if that’s what you want;

[lua]SWEP.Force = RNG[math.random(1, #RNG)][/lua]

Why are you defining it as a SWEP value? Is this number supposed to change each auto-refresh, each weapon, or when?

Is it a value between 12 and 200 or is it either one or the other?

Give us some specifics so we can give you help in resolving the issue.

what i wanted was either 200 or 14, so i could see if either one triggered before i know the code works.
here is the full code for the swep



if (SERVER) then
	AddCSLuaFile("shared.lua")
	SWEP.HoldType = "pistol"
	
	sound.Add( {
	name = "dear_sister_sound", 
	channel = CHAN_item, 
	volume = 1.0, 
	level = 80, 
	pitch = { 100 }, 
	sound = "weapons/dearsister.wav"
} )
end



if ( CLIENT ) then

	SWEP.PrintName		= "Dear Sister gun thingy"			
	SWEP.Author			= "Face"
	SWEP.Slot			= 1
	SWEP.SlotPos		= 4
	SWEP.IconLetter		= "e"
	SWEP.Instructions 	= "Left Click for shooting kleiner and Right Click for slowmo/music"
	SWEP.Purpose 		= "destroy all kleiners you see";
	resource.AddFile( "materials/vgui/entities/dearsister.vtf" )
	resource.AddFile( "materials/vgui/entities/dearsister.vmt" )
	resource.AddFile( "sound/weapons/dearsister.wav" )
	end

RNG = { 1 , 200 }
	
SWEP.Base			= "weapon_ironsight_base"

SWEP.Spawnable			= true
SWEP.AdminOnly			= true

SWEP.Category			= "my own shitS"
SWEP.ViewModel			= "models/weapons/v_pist_deagle.mdl"
SWEP.WorldModel			= "models/weapons/w_pist_deagle.mdl"
SWEP.ViewModelFlip           	= true
SWEP.DrawCrosshair		= true;

SWEP.Weight			= 3
SWEP.AutoSwitchTo		= false
SWEP.AutoSwitchFrom		= false


SWEP.TracerFreq			= 1 
SWEP.SwayScale			= 2
SWEP.BobScale 			= 2

SWEP.ForceApply = RNG[math.random(1, #RNG)]

SWEP.Primary.Recoil		= 3
SWEP.Primary.RecoilXRand	= 1.5
SWEP.Primary.RecoilYRand	= 1.1
SWEP.Primary.Damage		= 9999999999999999999999
SWEP.Primary.NumShots		= 1
SWEP.Primary.Cone		= 0
SWEP.Primary.ClipSize		= 6
SWEP.Primary.Delay		= 0.02
SWEP.Primary.DefaultClip	= 70
SWEP.Primary.Automatic		= false
SWEP.Primary.Ammo			= "357"


SWEP.Secondary.Sound		= "dear_sister_sound"
SWEP.Secondary.ClipSize = 1
SWEP.Secondary.Delay = 46
SWEP.Secondary.DefaultClip = 1
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo = "none"

SWEP.IronSightsPos 		= Vector( 5.15, -2, 2.6 )
SWEP.IronSightsAng 		= Vector( 0, 0, 0 )
function SWEP:Deploy()
	RunConsoleCommand( "pp_motionblur_addalpha", "0.5") 
	RunConsoleCommand( "pp_motionblur_delay", "0.0")
	RunConsoleCommand( "pp_motionblur_drawalpha", "1")
end
function SWEP:PrimaryAttack()

if ( !self:CanPrimaryAttack() ) then return end
	self.Weapon:SetNextPrimaryFire( CurTime() + self.Primary.Delay )
	
	if ( !self:CanPrimaryAttack() ) then return end
	self.Weapon:EmitSound( self.Primary.Sound )
	self:CSShootBullet( self.Primary.Damage, self.Primary.Recoil, self.Primary.NumShots, self.Primary.Cone )

	self.Owner:ViewPunch( Angle( math.Rand(-self.Primary.RecoilXRand,-self.Primary.RecoilXRand) * self.Primary.Recoil, math.Rand(self.Primary.RecoilYRand,-self.Primary.RecoilYRand) *self.Primary.Recoil, 0 ) )
end

function SWEP:SecondaryAttack()

if ( !self:CanSecondaryAttack() ) then return end



	RunConsoleCommand( "pp_motionblur", "1" )

if (SERVER) then
	timer.Simple(0, function() game.SetTimeScale( 0.10 ) end )
	timer.Simple(3.2, function() game.SetTimeScale( 1 ) end )
end

	self.Weapon:SetNextSecondaryFire( CurTime() + self.Primary.Delay )
	
	if ( !self:CanSecondaryAttack() ) then return end
	self.Weapon:EmitSound( self.Secondary.Sound )
	
end

function SWEP:Think()
end


im not sure what the hash does and how that line of code is supposed to work because i still lack some basic lua skills, can someone explain to me how that works?

Ok, if you want one or the other then you can use table.Random( { 14, 200 } ), or math.random( 1, #_tab ); where _tab is { 14, 200 }…

The # is like table.Count, only it only counts numerical keys as +1. Anything non-numeric as a key gets skipped which is where table.Count comes in…

The # is to make things like Author. posted, so you can get a random value from key 1 to the last numerical key ( because you’ve got 2 elements, it means either one or the other ).

There are many different ways to randomize but there are a few that are better than others. Having a clearer picture of what you need helps us provide better answers.

Thank you so much! you helped me so much on this lua file, i have learnt alot on lua thanks to you.