Need help with randomizer script that gives tools

So the script is supposed to give out guns from a folder in replicated storage and there are three guns with diffrent attachments but same name so the script keeps giving me the same gun every time so how can I fix that without having to change the name of guns

Here is the script:

local RS = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Folder = RS:WaitForChild("Primary")

local function onCharacterAdded(character)
	wait(0)
	local Tools = Folder:GetChildren()
	local ToolsTable = Tools
	local chosenTools = {}

	repeat
		local selectedIndex = math.random(1, #ToolsTable)
		table.insert(chosenTools, ToolsTable[selectedIndex])
		table.remove(ToolsTable, selectedIndex)
	until #chosenTools >= 1
	-- Numbers of tools going to give

	local plr = Players:GetPlayerFromCharacter(character)
	for i,v in pairs(chosenTools) do
		Folder:FindFirstChild(v.Name):Clone().Parent = plr.Backpack
	end
end

local function onAdded(player)
	player.CharacterAdded:Connect(onCharacterAdded) -- Triggers when the player spawns or respawns
end

Players.PlayerAdded:Connect(onAdded)

Please help and thanks very much, if what I explained isn’t very understandable then apologies

2 Likes

Have a variable that stores the previous tool and compare with the new tool to ensure it isnt the same as last time:

local previousTools = {}
local function onCharacterAdded(character)
	task.wait(0)
    local plr = Players:GetPlayerFromCharacter(character)
	local Tools = Folder:GetChildren()
	local ToolsTable = Tools
    local previousTool= previousTools[plr] --old tool index
	local chosenTools = {}

	repeat
	   local selectedIndex = math.random(1, #ToolsTable)
       if  previousTool~=selectedIndex  then --check difference if its same tool or not
         previousTools[plr]=selectedIndex
		 table.insert(chosenTools, ToolsTable[selectedIndex])
		 table.remove(ToolsTable, selectedIndex)
       end
	until #chosenTools >= 1
	-- Numbers of tools going to give

	for i,v in pairs(chosenTools) do
		Folder:FindFirstChild(v.Name):Clone().Parent = plr.Backpack
	end
end
1 Like

Thanks, though I’m not that good at coding and the script doesn’t work so could you explain what I have to do please?

just change your part of the script i remade with mine.I only rewrote a section of your code thats why it will break for you.I gave you the full script down below:

1 Like
local RS = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Folder = RS:WaitForChild("Primary")

local previousTools = {}
local function onCharacterAdded(character)
	task.wait(0)
    local plr = Players:GetPlayerFromCharacter(character)
	local Tools = Folder:GetChildren()
	local ToolsTable = Tools
    local previousTool= previousTools[plr] --old tool index
	local chosenTools = {}

	repeat
	   local selectedIndex = math.random(1, #ToolsTable)
       if  previousTool~=selectedIndex  then --check difference if its same tool or not
         previousTools[plr]=selectedIndex
		 table.insert(chosenTools, ToolsTable[selectedIndex])
		 table.remove(ToolsTable, selectedIndex)
       end
	until #chosenTools >= 1
	-- Numbers of tools going to give

	for i,v in pairs(chosenTools) do
		Folder:FindFirstChild(v.Name):Clone().Parent = plr.Backpack
	end
end

local function onAdded(player)
	player.CharacterAdded:Connect(onCharacterAdded) -- Triggers when the player spawns or respawns
end

Players.PlayerAdded:Connect(onAdded)
1 Like

It still gives out the same tool so I don’t know how to fix that but thanks for trying to help. Maybe should I use tagging or values to divide the tools?

oh sorry my code should work i read your issue wrong had to just clone the tool because same name would bug it

1 Like

Try this now it should work:

local RS = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Folder = RS:WaitForChild("Primary")

local previousTools = {}
local function onCharacterAdded(character)
	task.wait(0)
    local plr = Players:GetPlayerFromCharacter(character)
	local Tools = Folder:GetChildren()
	local ToolsTable = Tools
    local previousTool= previousTools[plr] --old tool index
	local chosenTools = {}

	repeat
	   local selectedIndex = math.random(1, #ToolsTable)
       if  previousTool~=selectedIndex  then --check difference if its same tool or not
         previousTools[plr]=selectedIndex
		 table.insert(chosenTools, ToolsTable[selectedIndex])
		 table.remove(ToolsTable, selectedIndex)
       end
	until #chosenTools >= 1
	-- Numbers of tools going to give

	for i,v in pairs(chosenTools) do
		v:Clone().Parent = plr.Backpack
	end
end

local function onAdded(player)
	player.CharacterAdded:Connect(onCharacterAdded) -- Triggers when the player spawns or respawns
end

Players.PlayerAdded:Connect(onAdded)
1 Like

It works, thank you very much and sorry for the trouble

no problem i was at fault myself for not reading your issue fully.i read it partially lol.

1 Like