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?
@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
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.
@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.
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
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.
EDIT: Added missing ‘)’ at end of function connections
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.
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)