How would you make a melee specific randomizer?

A while back, I tried to make a script that gives you a random melee weapon on spawn. It basically went with /Pick number between 1-34. If 1 give player tool1. Ifelse 2 give player tool2\ and so on.

I tried example scripts and they never worked out for me. I don’t actually know how to get this to work. Does it have anything to do with Script vs. LocalScript?

Where are the weapons stored?

If they were stored in Backpack for example:

local tools = backpack:GetChildren()
local randomTool  = tools[math.random(1,#tools)]:Clone()

@njesk12
I stored them in ReplicatedStorage for testing purposes. Once I figure this out I will clone them instead of a direct path.
local tools = ReplicatedStorage
local random = math.random(1, 34)
if 1 then
tools.tool1.Parent = script.Parent

So you would use

local tools = game:GetService("ReplicatedStorage"):GetChildren()
local randomTool = tools[math.random(1,#tools)] -- :Clone() is optional but recommended
randomTool.Parent = script.Parent

EDIT: Also recommend doing this from a server script.

EDIT2: This also assumes that there are ONLY tools in the replicated storage. If there are other objects, this would need some editing. If you set the tools in a folder in replicatedstorage that would be much better as then the bin would be much more controlled.

1 Like

Thanks for the fast response! I will test this out and I think I am going to put the tools in a folder!

1 Like

@njesk12
This is probably my fault but, it didn’t work. I started Developing about a month ago and I’m super new to everything. I put it in a folder and it didn’t work. I need a little help getting your script set-up.

So it might be because the directory wasn’t changed in the script. What is the path to the folder?

game.ReplicatedStorage.Stabbers --I have all my tools in here

change this from the old script

local tools = game:GetService("ReplicatedStorage"):WaitForChild("Stabbers"):GetChildren()

Still nothing. Some images:
Screenshot 2021-03-20 195438

Could I see your source code? Is this being ran from a script or localscript? And is there any errors being output? Where is the script located? Is it Enabled?

It’s a lot of questions but I would like to know as much as I can to help :slight_smile:

It’s no problem, ask away. About your questions, I’m confused about what you mean by source code. This is a script. There are no errors there are just no results. It is in ServerScriptService. It is enabled. Did sourcecode mean all of my scripts?

Source code is just what you write in your script. I think I know what the error might be, though.

local replicatedStorage = game:GetService ("ReplicatedStorage")
local tools = replicatedStorage:WaitForChild ("Stabbers")

function giveRandomTool(backpack)
   local _tools = tools:GetChildren() -- NOTE: '_tools' is not the same as 'tools'
   local randomTool = _tools[math.random(1,#_tools)]:Clone() -- we clone because we don't want the tool to be lost forever after giving it away
   randomTool.Parent = backpack
end

-- I believe the problem was there was no connection to carry out the action, so nothing was done in the end
game.Players.PlayerAdded:Connect (function (player)
   player.CharacterAdded:Connect (function (character)
      local backpack = player:WaitForChild ("Backpack")
      giveRandomTool (backpack)  -- we insert into backpack everytime character spawns
   end)
end)

P.S. The code I just gave you is what’s called Source Code. :slight_smile:

EDIT: Added missing ‘)’ at end of function connections

EDIT2: Changed folder name to ‘Stabbers’

So, I have a Gui, that until it’s dismissed, the Roblox bar and backpack are disabled. Will that interfere with your code?
Edit 1: If not than I’ll give you Solve Credit or whatever it’s called.
Edit 2: Upon inserting this unto studio script, there seems to be an error on the final end line. Is this just a bug/glitch?
Edit 3: I found it. The first end line needed a ). I’m proud of myself.

1 Like

Ahh yes, so after the tool has been sent to backpack, you’d also have to create a custom backpack of sorts. Or if your game consists of just having one tool, you can simply create a RemoteEvent that the server fires to the client to tell them they have a Tool now. Then the player can equip it from the backpack themselves.

You missed ‘)’ to close the :Connect() call.
Change your code to:

local replicatedStorage = game:GetService ("ReplicatedStorage")
local tools = replicatedStorage:WaitForChild ("Tools")

function giveRandomTool(backpack)
   local _tools = tools:GetChildren() -- NOTE: '_tools' is not the same as 'tools'
   local randomTool = _tools[math.random(1,#_tools)]:Clone() -- we clone because we don't want the tool to be lost forever after giving it away
   randomTool.Parent = backpack
end

-- I believe the problem was there was no connection to carry out the action, so nothing was done in the end
game.Players.PlayerAdded:Connect (function (player)
   player.CharacterAdded:Connect (function (character)
      local backpack = player:WaitForChild ("Backpack")
      giveRandomTool (backpack)  -- we insert into backpack everytime character spawns
   end
end)

Note that @CodeOverloader had already mentioned that.

1 Like

Is it on the first or second end?

Both actually sorry about that!

Note: The backpack is re-enabled after gui is gone. Is this still the same thing?

It is supposed to be at the first end there is in the Connect() call’s function.

Event:Connect(function() end) -- // In the first end.