Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why doesn't this script give all players a different weapon?

Asked by 5 years ago
Edited 5 years ago

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!

1
you could add a function so that when a player receives a weapon it checks the weapons in all other players and then based on math.random fires the respective client to change their weapon? By the way is this server side? SerpentineKing 3885 — 5y
0
Yes, this is serverside. MustangHeart 67 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago

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
0
Thanks! This worked like a charm. MustangHeart 67 — 5y
0
Dang it User#24403 69 — 5y
Ad
Log in to vote
1
Answered by 5 years ago

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

Answer this question