Help with random tool giver

local selectedIndex = math.random(1, #ToolsTable):Clone()
selectedIndex.Parent = game.Workspace

You’re generating a random number and then attempting to clone it.

local RandomIndex = math.random(1, #ToolsTable)
local RandomTool = ToolsTable[RandomIndex]:Clone()
RandomTool.Parent = --Assign parent.

i tried this method and it worked until the second print, this ended up happening
image

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

Oh, that error is completely unrelated.

local plr = Players:GetPlayerFromCharacter()

You can’t call this method without arguments.

1 Like

what arguments can I call? Would player.name work? or something of the like?

local RS = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Folder = RS:WaitForChild("D6Items")
 
script.Parent.Activated:Connect(function()
	local Tools = Folder:GetChildren()
	local ToolsTable = Tools
	local chosenTools = {}

	repeat
		local selectedIndex = math.random(1, #ToolsTable)
		table.insert(chosenTools, ToolsTable[selectedIndex].Name)
		table.remove(ToolsTable, selectedIndex)
	until #chosenTools >= 1

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

Simple fix! Enjoy! :grin:

well, i used your code and nothing happened

i’m not even sure what’s happening anymore, it should be working and I don’t know why it isn’t

I updated it like one minute ago. Are you sure ToolsInTable is not nil?

yeah, I’m sure of it. Everything should be working, but I don’t know why it isn’t

Have you tried adding prints around the script? (Also, if you could loop through ToolsInTable and print all the variables, that would be amazing.)

Let me know your result!

for _, v in ipairs(ToolsInTable) do
    print(v.Name or nil)
end

Have you tried adding prints around the script?

I have, yes. And it’s shown further up the thread
I replaced the line

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

with yours, and still the same result. No errors, no prints, nothing

So ChosenTools is nil. Try this:

local RS = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Folder = RS:WaitForChild("D6Items")
 
script.Parent.Activated:Connect(function()
	local Tools = Folder:GetChildren()
	local ToolsTable = {}
	local chosenTools = {}

    for Index, Tool in ipairs(Tools) do
        if Tool:IsA("Tool") then
            table.insert(ToolsInTable, Tool)
        end
    end

	repeat
		local selectedIndex = math.random(1, #ToolsTable)
		table.insert(chosenTools, ToolsTable[selectedIndex].Name)
		table.remove(ToolsTable, selectedIndex)
	until #chosenTools >= 1

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

I have found an issue, and I don’t know why it happens. But here’s the problem:

I swapped the script.Parent.Activated with script.Parent.Equipped and it worked perfectly fine, I have no idea why it wasn’t working with Activated

regardless, thank you for helping me out with this

1 Like

Yeah, no problem! Now that I think about this, equipped will run before activated, so activated wasn’t running because equipped wasn’t running…