Help with random tool giver

Hi, so I’m going to try and be as clear as possible when explaining this because I had trouble trying to word it at all

So what I’m trying to do here is make a tool, a die tool, that when rolled gives you a random item from a folder in ReplicatedStorage. Right now I’m just trying to get the basic thing done and then work on cooldowns and stuff

The main issue is that, well, it isn’t working at all. It does not give me any errors either and printing doesn’t seem to be working for debugging either.

This is what it looks like in the explorer
image
image
(Excuse the use of free tools/models, it’s temporary for testing)

and this is what the script for the D6 itself looks 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])
		table.remove(ToolsTable, selectedIndex)
	until #chosenTools >= 1

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

Any help is appreciated. Thank you in advance

2 Likes

Put it in server storage and give it a try again.

1 Like

Try to print out some values/things in each step, for example:

1.Try print out before the repeat loop.
2.Try print out after the repeat loop.

And see what returns.
Also,
if you want to remove elements from a table, you have to do it like this -

table.remove(chosenTools,table.find(chosenTools,seletectedIndex)

[Might need to adjust it a bit]

1 Like

I put the tools folder in server storage and changed the code a little and it still gives the same result.

1 Like

I’ll will try to fix this for you I’ll be in studio

I modified my code to go with yours and added print statements, still, nothing happened. Nothing prints at all when the tool activates and I’m not getting any errors

Modified code
local SS = game:GetService("ServerStorage")
local Players = game:GetService("Players")
local Folder = SS:WaitForChild("D6Items")
 
script.Parent.Activated:Connect(function()
	local Tools = Folder:GetChildren()
	local ToolsTable = Tools
	local chosenTools = {}
	
	print("Works up until here")
	repeat
		local selectedIndex = math.random(1, #ToolsTable)
		table.insert(chosenTools, ToolsTable[selectedIndex])
		table.remove(chosenTools,table.find(chosenTools[selectedIndex]))
	until #chosenTools >= 1
	
	print("Will it work...")

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

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

Remove the Repeat and Until then give this code a try instead.

Same old result, no errors, no prints, nothing.

Code
local SS = game:GetService("ServerStorage")
local Players = game:GetService("Players")
local Folder = SS:WaitForChild("D6Items")
 
script.Parent.Activated:Connect(function()
	local Tools = Folder:GetChildren()
	local ToolsTable = Tools
	local chosenTools = {}
	
	print("Works up until here")
	
	local selectedIndex = math.random(1, #ToolsTable):Clone()
	selectedIndex.Parent = game.Workspace
	
	print("Will it work...")

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

wait so it’s not even printing the first line of “Works up until here”?

nope, and it’s not giving any errors either

Then that’s a different error as of now then. Check why your tool is not Activated

how am I able to check if my tool is activated?

do

Equipped:Connect

instead of

Activated:Connect

What you should do is implement a randomizer system once u die you get a new tool. then the random tool giver respawns your character

that’s not my goal here. I am trying to make a tool itself give you another random item

In that image you’re detecting logs from the client, the server is running this code, so click on the Server button right next to Client in your output

I imagine everything is working fine, you’re actually just setting the parent of your tools to nil since you didn’t provide your character to the GetPlayerFromCharacter method

EDIT: You also call Clone on the return result of math.random, make sure you get your instance beforehand

okay, so now we’re getting somewhere

try your old codes and see if they work, if not then uh ig retry other methods

well that’s a bit of a vague answer

I ment retry you codes that originally you put before I told you to change it and see if they work. if not then you can tell us.