Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I limit it so that players only get one of each gear?

So in this game I'm working on, I have a script that randomly hands out 6 gear to a player, but sometimes it'll 2 or 3 of the same gear to a player, so they're left with three of the same gear and three other things to use (just an example). So my question is, using the script provided below, how would I limit it so that a player can only get one of each gear, so that theyre not ending up with more than one of the same gear?

gears = {1145081326, 1208300505, 34398938, 186867645, 10758456, 298087401, 243007180, 212296936, 116693764, 48596305, 72713855, 118281463, 107458483, 11999247, 11377306, 11563251, 115377964, 225921000, 90718505, 2103274863, 1180418251, 66426498, 566780253, 95951330, 846792499, 11999279, 972189904, 1046323916, 391005275, 928806888, 169602103, 78730532, 130112971, 1492225511, 208659586}

game.Players.PlayerAdded:connect(function(ply)
    ply.CharacterAdded:Connect(function()
        for i = 1,6 do
            local model = game:GetService("InsertService"):LoadAsset(gears[math.random(#gears)])    
            for _,v in pairs(model:GetChildren()) do
                if v:IsA("Tool") or v:IsA("HopperBin") then
                    v.Parent = ply.Backpack
                end
            end
        end
    end)
end)

2 answers

✓ Accepted answer

Ok I added a table with all the gears taken local taken = {} The getGear function generates a new id and checks if it is taken, if so it generates a new one.

local gears = {1145081326, 1208300505, 34398938, 186867645, 10758456, 298087401, 243007180, 212296936, 116693764, 48596305, 72713855, 118281463, 107458483, 11999247, 11377306, 11563251, 115377964, 225921000, 90718505, 2103274863, 1180418251, 66426498, 566780253, 95951330, 846792499, 11999279, 972189904, 1046323916, 391005275, 928806888, 169602103, 78730532, 130112971, 1492225511, 208659586}

game.Players.PlayerAdded:connect(function(ply)
    ply.CharacterAdded:Connect(function()
	local taken = {}
	local function getGear()
		local id = math.random(1,#gears)
		while taken[id] do
			id = math.random(1,#gears)
		end
		taken[id] = true
		local model = game:GetService("InsertService"):LoadAsset(gears[id])
		return model
	end
        for i = 1,6 do
	    local model = getGear()
            for _,v in pairs(model:GetChildren()) do
                if v:IsA("Tool") or v:IsA("HopperBin") then
        		v.Parent = ply.Backpack
        	end
            end
        end
    end)
end)

1 comment

0

This worked! Thank you very much

Just doing this off the top of my head so if it's wrong, sorry.

if game.Players.LocalPlayer.Backpack[gears] then
	--script
else
	--Do nothing if it is already there.
end

1 comment

0

FindFirstChild() would be more useful in this situation, but this works too I guess

This discussion is preserved as it was. New answers, comments, and votes are closed.