TTT btw
I'm making it so if someone doesn't have a secondary, it gives them one from a table. (custom round thing)
I made a table with about 12 options in it.
on round start this is used:
Ply:Give(table.Random(pistol_weapon))
It works. Sometimes.
So I tried a timer as well, every 3 seconds it would give someone a secondary if they don't have one
using the same command as above but some players will normally get a weapon after 10~ seconds.
I also tried
pistol_weapon[math.random(1, #pistol_weapon)])
but again it took a while
Can we see the code?
table.Random goes through the table, while the key indexing doesn't
This is from the power round addon. Now I'm going to post this because I assume I can. The information being posted is in the screenshots on the gmodstore page apart from custom crappy code I made. (Before asked if I should contact the owner for support, it says they will not help with custom scripts)
PowerRounds.AddRound({
Name = "Pistols Only",
Gamemode = "TTT",
NameClr = Color(191, 12, 12),
Description = "Pistols Only, drop weapon to get a new one (delay to prevent spamming) Normal TTT Rules Apply.",
DescriptionClr = Color(191, 12, 12),
PlayersStart = function(Ply)
pistol_weapon = {
"weapon_zm_revolver",
"weapon_ttt_glock",
"weapon_ttt_p228",
"weapon_zm_pistol",
"weapon_ttt_a3000",
"weapon_ttt_pistol",
"weapon_ttt_tflippy_tec9",
"weapon_ttt_elites",
"weapon_ttt_tflippy_rbull",
"weapon_ttt_tflippy_luger"
}
Ply:StripWeapons()
//print(table.Random(pistol_weapon))
Ply:Give(table.Random(pistol_weapon))
end,
PlayerCanPickupWeapon = function(Ply, Ent)
for _,v in RandomPairs(pistol_weapon) do
//print(v)
return Ent:GetClass() == v
end
end,
STIMER_1_GIVEPISTOL = function()
for k,v in ipairs(PowerRounds.Players(2) ) do
if !v:HasWeapon("weapon_zm_revolver") and !v:HasWeapon("weapon_ttt_glock") and !v:HasWeapon("weapon_ttt_p228")
and !v:HasWeapon("weapon_zm_pistol") and !v:HasWeapon("weapon_ttt_a3000") and !v:HasWeapon("weapon_ttt_tflippy_rbull")
and !v:HasWeapon("weapon_ttt_pistol") and !v:HasWeapon("weapon_ttt_tflippy_tec9") and !v:HasWeapon("weapon_ttt_elites")
and !v:HasWeapon("weapon_ttt_tflippy_luger") or v:HasWeapon("weapon_ttt_unarmed") then
// sorry looks like crap. lol
v:Give(table.Random(pistol_weapon))
end
for q,p in pairs(v:GetWeapons() ) do
kind = p.Kind
ammo = string.lower(p.Primary.Ammo)
white = {
"pistol",
"smg1",
"alyxgun",
"357",
"buckshot"
}
if kind == WEAPON_PISTOL and table.HasValue(white, ammo) then
v:SetAmmo(p.Primary.ClipMax, ammo)
end
end
end
end
})
For the STIMER I was trying to make it so if a player drops their weapon; aka if they don't have one on the table they will be given one. Sorry it looks like garbage, but it was the only way I knew how to make it work
/*STIMER_5_GIVEPISTOL = function()
for k,v in ipairs(PowerRounds.Players(2) ) do
local tableString = table.ToString(pistol_weapon)
if !v:HasWeapon(tableString) then
v:Give(table.Random(pistol_weapon))
end
end
end,*/
This is bad code for many reasons, but it shouldn't take 5 seconds to run. Whats the length of PowerRounds.Players(2) ?
Not going to post the full code (Not sure i'm allowed as it's part of a paid addon, if its allowed lemme know and I'll post it)
This is its usage:
--[[PowerRounds.Players Usage:
Arguments:
Type number Type ID of who to get: 1 = All, 2 = Alive(Without SpecDM), 3 = Alive(All alive), 4 = Only alive muderers/traitors/hunters/guards(Without SpecDM), 5 = Only alive bystanders/innocents/detectives/props/prisoners(Without SpecDM), 6 = All muderers/traitors/hunters/guards, 7 = All bystanders/innocents/detectives/props/prisoners
(OPTIONAL)Player object OR table of Player objects Player(s) to exclude from returned list.
Returns:
Table of player objects
]]
the function is:
function PowerRounds.Players(Type, Exclude)
I don't know.
local tableString = table.ToString(pistol_weapon)
This should be outside the loop, and you should add:
local pistol_weapon = pistol_weapon
As the first line of your function. But as I said, I don't see why that would take 5 seconds to run. Looks like the problem is somewhere else.
I have no idea why it's taking 5 seconds. You did mention it's a TTT server so it could be lag being thrown into the equation, I don't know. Also, take one look at the development section and you see so many people having problems with TTT right now, especially that one with the 20 coin bounty.
I'm not sure if this would affect anything but I'm testing it on a LocalServer using peer to peer with a friend and spawned in 6 other bots.
Also, under the PlayersStart function I added a print to try and debug things.
print(Ply:Give(table.Random(pistol_weapon)))
Now, this should print only 8 times considering there are 8 players on the server I assume?
But in console this is the results:
Weapon [14][weapon_ttt_p228]
Weapon [279][weapon_ttt_tflippy_rbull]
Weapon [281][weapon_ttt_pistol]
Weapon [283][weapon_ttt_tflippy_luger]
Weapon [285][weapon_ttt_tflippy_tec9]
Weapon [287][weapon_ttt_glock]
Weapon [12][weapon_zm_pistol]
Weapon [279][weapon_zm_pistol]
Weapon [281][weapon_zm_revolver]
Weapon [283][weapon_zm_pistol]
Weapon [286][weapon_ttt_elites]
Weapon [288][weapon_ttt_tflippy_rbull]
Weapon [290][weapon_ttt_glock]
Weapon [12][weapon_zm_pistol]
Weapon [280][weapon_zm_revolver]
Weapon [282][weapon_ttt_tflippy_tec9]
Weapon [12][weapon_ttt_tflippy_tec9]
Weapon [281][weapon_ttt_elites]
Weapon [283][weapon_zm_pistol]
Weapon [286][weapon_ttt_a3000]
Weapon [12][weapon_ttt_tflippy_rbull]
Weapon [12][weapon_ttt_a3000]
Weapon [281][weapon_ttt_elites]
Weapon [12][weapon_ttt_p228]
Weapon [12][weapon_ttt_tflippy_luger]
Weapon [281][weapon_ttt_a3000]
Weapon [12][weapon_ttt_tflippy_rbull]
Weapon [12][weapon_ttt_tflippy_rbull]
Weapon [281][weapon_ttt_glock]
It keeps running until everyone has a gun and then stops.
Try the PlayerSpawn hook, afaik the hook you used repeats multiple times for every player.
I already explained why your HasWeapon will always return false, so yeah, everybody gets a gun.
PlayersStart runs for each player on round start
STimer is a server timer that runs every x second where x is whatever I specify
I'm not sure how I would make a PlayerSpawn Hook work since the round activates 15 seconds after players initially spawn.
It's completely random, sometimes I will get a weapon within 1 second, others it can be up to 10.
Also, those are the weapon class names specified in the weapon list.
I'm trying to make it so players can only pickup those guns, so it is a secondary only round. Also, it is being used 1 by 1 as far as I know in the function:
STIMER_1_GIVEPISTOL = function()
There's no way table.random takes a second to run...If your table is ordered just do weaponList[math.random(1,#weaponList)]
Just as a proof of concept, not a permanent solution.
And the uncertainty in the amount of time it takes is really odd. This really shouldn't happen. If you can, try it in Sandbox.
I'll try it on sandbox whenever I get back.
I uploaded a small clip to try and explain what I meant.
There should be 8 lines in console on round start as there are 8 players on but it just keeps repeating.
If I drop a gun, it takes a few attempts before it gives me another one.
https://youtu.be/7ySA_T4QUN8
The function has to use weapon class names to work.
I tried doing:
PlayerCanPickupWeapon = function(Ply, Ent)
for k,v in pairs(pistol_weapon) do
if table.HasValue(pistol_weapon, v) then
return true
else
return false
end
end
end,
but I am receiving 2 pistols and can pickup pirmarys. I'm a little clueless on how to do this
The function used is this:
PlayerCanPickupWeapon = function(Ply, Ent) end, --Function that will run when a person gets a gun {Default: empty function} [Values: Ply = player, Ent = weapon] (Return: true = will let picking up, false = will not)
Also, big thank you to everyone who replied and helped me out on this thread in case I forgot to mention it!
H O O K
hook.Add("PlayerCanPickupWeapon", "canpickupweapon", function(ply,wep)
if table.HasValue(pistol_weapon, v) then
-- Check if it is "secondary" here
return true
else
return false
end
end )
And what do you mean by "Primary"? You mean weapons without secondary fire or do you have some kind of dual wielding script thing. If so, send me that.
I made it somewhat work like this:
PlayerCanPickupWeapon = function(Ply, Ent)
if table.HasValue(pistol_weapon, Ent:GetClass() ) then
return true
else
return false
end
end,
Again its spawning 2 secondaries. (By secondary I meant like pistols)
But that's okay. I can try to fix it with something else I wanted to do.
And that is only being able to wield 1 weapon from SWEP.Slot 2 (at the moment you can pick anything up)
You need to use it in the hook. Hooks are called on an event (in this case when a player PICKS UP A WEAPON)
Put this code in the file, just by itself in the file somewhere. It will work.
hook.Add("PlayerCanPickupWeapon", "uniquename", function(ply,wep)
if table.HasValue(pistol_weapon, v) then
if wep:GetSlot() >= 1 then return false end -- Slot numbers start at 0
return true
else
return false
end
end )
I just found another way to make it work too haha, was just about to come and edit my old reply.
PlayerCanPickupWeapon = function(Ply, Ent)
local limit = 1
if table.HasValue(pistol_weapon, Ent:GetClass() ) and #Ply:GetWeapons() < limit then
return true
else
return false
end
end,
That made it work.
Thank you very much for all of your help! I don't think I could've figured it on my own haha
When you make local variables when you don't need to make local variables
PlayerCanPickupWeapon = function(Ply, Ent)
if table.HasValue(pistol_weapon, Ent:GetClass() ) and #Ply:GetWeapons() < 1 then
return true
else
return false
end
end,
Just because a weapon uses 'pistol' hold type does not mean it has to be a pistol. Example,many dual pistols/smgs/rifles use 'duel' hold type, despite only one of them being a pistol. If he wants to determine whether a weapon is a pistol or not, I don't think there is any other way than just defining a table with the pistol weapons manually.
Sorry, you need to Log In to post a reply to this thread.