Same script, but this should be the last kink to work out. So whenever the character spawns it is supposed to give each one a random weapon, but never the same one. Sometimes it gives two players the same weapon and another player no weapon! I can assume a way to solve this is by using tables, but I need more training with those. So can anyone help me?
--Framework for Random Royale --With help from scriptinghelpers.org and Hadschi --//Variables local rs = game:GetService("ReplicatedStorage") local maps = rs:FindFirstChild("Maps") local contestants = game.Players:GetPlayers() local gamewep = rs:FindFirstChild("GameWep") local mapholder = workspace.MapHolder --//Timing local intermission = 15 --change to 30 after testing local roundtime = 180 --maybe lengthen or shorten local endtime = 10 --maybe 5 seconds --//Weapon System local folderclone = gamewep:Clone() folderclone.Name = "NewGameWep" folderclone.Parent = rs local folderchildren = folderclone:GetChildren() while true do mapholder:ClearAllChildren() wait(intermission) local contestants = game.Players:GetPlayers() local allmaps = maps:GetChildren() local newmap = allmaps[math.random(1,#allmaps)] local clonemap = newmap:Clone() clonemap.Parent = workspace.MapHolder print ("Map Loaded") wait(2) local spawns = clonemap:WaitForChild("Spawns") for _,player in pairs(game.Players:GetPlayers()) do if player and player.character then local spawnplace = spawns.Spawn local torso = player.Character:WaitForChild("Torso") if torso then torso.CFrame = CFrame.new(spawnplace.Position + Vector3.new(0,3,0)) local checktag = Instance.new("StringValue") checktag.Name = "CheckTag" checktag.Parent = player.Character print ("Players Spawned") local backpack = player:FindFirstChild("Backpack") local randomwep = folderchildren[math.random(1,#folderchildren)] randomwep.Parent = player.Backpack end end end --last man standing local timer = roundtime while timer > 0 do wait(1) timer = timer - 1 activecontestants = {} for _,player in pairs(contestants) do if player then local character = player.Character if character then local checktag = character:FindFirstChild("CheckTag") local humanoid = character:FindFirstChild("Humanoid") if checktag and humanoid and humanoid.Health > 0 then table.insert(activecontestants, player) end end end end if #activecontestants <=1 then break end end folderclone:destroy() print ("Game End") --restart end
Giving players the weapons starts at about line 39. Any and all help will be appreciated!
To avoid getting the same weapon, you could remove chosen weapons. Since your options are given in a table
, table.remove
would be enough to remove those options for you.
-- table.remove(table, index) local tbl = {"first", "second", "third"} print(unpack(tbl)) -- outputs: first second third table.remove(tbl, 2) -- remove item in index 2 which is "second" print(unpack(tbl)) -- outputs: first third
Knowing this, we can apply it on the table folderclone:GetChildren()
returns.
-- line 52 on your script local randindex = math.random(1, #folderchildren) -- the index we will use for the table local randomwep = folderchildren[randindex] -- chose weapon table.remove(folderchildren, randindex) -- now we remove it so you can not choose the same weapon twice
Not sure why the other players do not get a weapon. It could be that they did not get past the and player.character
condition.
Now, for making sure two or more players do not get the same weapon, yes, you can use tables. Here is how I would do it:
local weaponTable = {unpack(folderchildren)} -- # so we do not make a reference to the folderchildren variable, make a copy for _, player in ipairs(game.Players:GetPlayers()) do table.remove(weaponTable):Clone().Parent = player.Backpack end
Of course you would add line 6 to the rest of your loop. And if it is confusing, do not worry. I will explain.
table.remove(list, pos)
removes and returns the element at position pos
, and if a gap is created by the removal, the elements to the right are shifted to the left to fill in the gap. If pos
is not provided it defaults to 1. So pretty much it is always removing the first index and since a gap is created, the things to the right are shifted to the left so there will always be an index 1 unless #tbl == 0