I wanted to do a Swep which plays a random sound on Leftclick
but i dont find the error:
if SERVER then
AddCSLuaFile( "shared.lua" )
end
if CLIENT then
SWEP.PrintName = "Memebox"
SWEP.Slot = 7
SWEP.ViewModelFOV = 60
SWEP.ViewModelFlip = false
end
SWEP.Base = "weapon_tttbase"
SWEP.HoldType = "slam"
SWEP.Primary.Delay= 1.0
SWEP.Primary.Recoil= 0
SWEP.Primary.Damage= 0
SWEP.Primary.NumShots= 1
SWEP.Primary.Cone= 0
SWEP.Primary.ClipSize= -1
SWEP.Primary.DefaultClip= -1
SWEP.Primary.Automatic = false
SWEP.Primary.Ammo = "none"
SWEP.Primary.Sound = Sound("Weapon_SLAM.SatchelThrow")
SWEP.ViewModel = "models/weapons/c_slam.mdl"
SWEP.WorldModel = "models/weapons/w_slam.mdl"
SWEP.UseHands = true
function SWEP:PrimaryAttack()local randomsounds = {
"weapons/Memebox/musik1.wav",
"weapons/Memebox/musik2.wav",
"weapons/Memebox/musik3.wav",
"weapons/Memebox/musik4.wav",
"weapons/Memebox/musik5.wav",
"weapons/Memebox/musik6.wav"
}
self.Owner:EmitSound(randomsounds [math.random(1,#randomsounds)
self.Owner:Remove()
end
SWEP.Kind = WEAPON_EQUIP2
SWEP.AutoSpawnable = false
SWEP.AmmoEnt = "item_ammo_slam_ttt"
SWEP.CanBuy = { ROLE_TRAITOR }
SWEP.InLoadoutFor = nil
SWEP.LimitedStock = false
SWEP.AllowDrop = true
SWEP.IsSilent = true
SWEP.NoSights = true
SWEP.Icon = "vgui/ttt/icon_boyx_memebox"
SWEP.EquipMenuData = {
type = "Weapon",
desc = "Plays a random Meme music to distract your enemies!"
};
end
resource.AddFile("materials/vgui/ttt/icon_boyx_memebox.vmt")
end
Can you help?
local randomsounds = {
"weapons/Memebox/musik1.wav",
"weapons/Memebox/musik2.wav",
"weapons/Memebox/musik3.wav",
"weapons/Memebox/musik4.wav",
"weapons/Memebox/musik5.wav",
"weapons/Memebox/musik6.wav"
}
local randomNum = math.floor(math.random(table.count(randomsounds))) // gets how many sounds are in the table and selects a random sound
local randomSound = randomsounds[randomNum] // assigns the variable to the sound path in the list
self.Owner:EmitSound(randomSound)
as long as everything else worked before adding in a sound this should work I did not test it.
First of all, thanks for quick answer.
Ive tested it but an error happened:
[ERROR] gamemodes/terrortown/entities/weapons/weapon_ttt_memebox/shared.lua:55: attempt to call field 'count' (a nil value)
1. unknown - addons/boyx memebox [ttt]/gamemodes/terrortown/entities/weapons/weapon_ttt_memebox/shared.lua:55
Idk what i did wrong.
Try replacing it with just this below and see if that fixes it - I'm newer to lua I'm not home to test and see why table.Count isn't returning a value..
local randomNum = math.floor(math.random(6))
Ok thank you it works. But i have a weird Bug that i need to press Left Mouse twice or more often to activate it. And then more than 1 Songs plays at the same time :P
Heres my current code:
---- Example TTT custom weapon
-- First some standard GMod stuff
if SERVER then
AddCSLuaFile( "shared.lua" )
end
if CLIENT then
SWEP.PrintName = "Memebox"
SWEP.Slot = 7 -- add 1 to get the slot number key
SWEP.ViewModelFOV = 60
SWEP.ViewModelFlip = false
end
-- Always derive from weapon_tttbase.
SWEP.Base = "weapon_tttbase"
--- Standard GMod values
SWEP.HoldType = "slam"
SWEP.Primary.Delay= 1.0
SWEP.Primary.Recoil= 0
SWEP.Primary.Damage= 0
SWEP.Primary.NumShots= 1
SWEP.Primary.Cone= 0
SWEP.Primary.ClipSize= -1
SWEP.Primary.DefaultClip= -1
SWEP.Primary.Automatic = false
SWEP.Primary.Ammo = "none"
SWEP.Primary.Sound = Sound("Weapon_SLAM.SatchelThrow")
SWEP.ViewModel = "models/weapons/c_slam.mdl"
SWEP.WorldModel = "models/weapons/w_slam.mdl"
SWEP.UseHands = true
function SWEP:PrimaryAttack()
local randomsounds = {
'weapons/Memebox/musik1.wav',
'weapons/Memebox/musik2.wav',
'weapons/Memebox/musik3.wav',
'weapons/Memebox/musik4.wav',
'weapons/Memebox/musik5.wav',
'weapons/Memebox/musik6.wav'
}
local randomNum = math.floor(math.random(6)) // gets how many sounds are in the table and selects a random sound
local randomSound = randomsounds[randomNum] // assigns the variable to the sound path in the list
self.Owner:EmitSound(randomSound)
self:Remove()
end
--- TTT config values
-- Kind specifies the category this weapon is in. Players can only carry one of
-- each. Can be: WEAPON_... MELEE, PISTOL, HEAVY, NADE, CARRY, EQUIP1, EQUIP2 or ROLE.
-- Matching SWEP.Slot values: 0 1 2 3 4 6 7 8
SWEP.Kind = WEAPON_EQUIP2
-- If AutoSpawnable is true and SWEP.Kind is not WEAPON_EQUIP1/2, then this gun can
-- be spawned as a random weapon. Of course this AK is special equipment so it won't,
-- but for the sake of example this is explicitly set to false anyway.
SWEP.AutoSpawnable = false
-- The AmmoEnt is the ammo entity that can be picked up when carrying this gun.
SWEP.AmmoEnt = "item_ammo_slam_ttt"
-- CanBuy is a table of ROLE_* entries like ROLE_TRAITOR and ROLE_DETECTIVE. If
-- a role is in this table, those players can buy this.
SWEP.CanBuy = { ROLE_TRAITOR }
-- InLoadoutFor is a table of ROLE_* entries that specifies which roles should
-- receive this weapon as soon as the round starts. In this case, none.
SWEP.InLoadoutFor = nil
-- If LimitedStock is true, you can only buy one per round.
SWEP.LimitedStock = false
-- If AllowDrop is false, players can't manually drop the gun with Q
SWEP.AllowDrop = true
-- If IsSilent is true, victims will not scream upon death.
SWEP.IsSilent = true
-- If NoSights is true, the weapon won't have ironsights
SWEP.NoSights = true
-- Equipment menu information is only needed on the client
if CLIENT then
-- Path to the icon material
SWEP.Icon = "vgui/ttt/icon_boyx_memebox"
-- Text shown in the equip menu
SWEP.EquipMenuData = {
type = "Weapon",
desc = "Plays a random Meme music to distract your enemies!"
};
end
-- Tell the server that it should download our icon to clients.
if SERVER then
-- It's important to give your icon a unique name. GMod does NOT check for
-- file differences, it only looks at the name. This means that if you have
-- an icon_ak47, and another server also has one, then players might see the
-- other server's dumb icon. Avoid this by using a unique name.
resource.AddFile("materials/vgui/ttt/icon_boyx_memebox.vmt")
end
I've never written a script for a SWEP so I don't know them entirely but as far as multiple sounds playing at the same time you'll have to stop the current one and play another one or add a delay if you want the entire song to play first.
self.Owner:EmitSound(randomsounds[math.random(#randomsounds)]
If you do not play sounds for the first time, make sure that they are installed in your game.
Don't use table.Count. Just get the length of the table using #randomsounds. It's a lot faster for indexed tables.
Just a small suggestion
I can see that your file names are increasing with a number in the name. Like ( sound2.wav, sound3.wav ). You could use this to your advantage like I did below.
local randomSound = "weapons/Memebox/musik" .. math.random( 1, 6 ) .. ".wav"
// Emit the sound
self.Owner:EmitSound( randomSound )
It basically cuts the string and changes the number to a random integer from 1 to 6. Do remember that math.random returns a integer while math.Rand returns a float value.
So the math.random function is basically this:
math.Round( math.Rand( 1, 6 ), 0 )
But if your file names are different than each other you could make a table with every sound path in them as a string. Then use the table.Random function to pick a random string in the table and then emit it.
local Sounds = { "weapons/Memebox/SomeSoundThing.wav", "weapons/Memebox/moresounds.wav", "weapons/sounds/beep.wav" }
local randomTableSound = table.Random( Sounds )
// Emit the sound
self.Owner:EmitSound( randomTableSound )
Any of these methods are much "cleaner" in my opinion.
I hope you find this useful!
But how do i fix that more than one sound is playing at the same time?
When you play a sound do stop sound first to stop the first sound.. I found the function by typing into google gmod lua stop sound
Entity/StopSound
Maybe a if statement with a timer?
No no. I have the problem that when i activate the Swep once it plays multiple sounds at the same time. When you left click it, it should play only one .wav and then delete it self but it plays multiple .wav and then delete itself
Sorry, you need to Log In to post a reply to this thread.