Hi,
Thank you for reading my question!
Currently, I have a script in a part with a clickdetector that inserts a tool into a player's backpack once the mouse is clicked.
tool = game.ServerStorage:FindFirstChild'CandyBar' debounce = true script.Parent.ClickDetector.MouseClick:Connect(function(plr) if debounce == true then debounce = false if plr.Backpack:FindFirstChild'CandyBar' or plr.Character:FindFirstChild'CandyBar' then debounce = true return false end tool:Clone().Parent = plr.Backpack wait(1) debounce = true end end)
I would like to add a folder in ServerStorage that contains many tools in it that are sent to the player's backpack randomly. I am trying to figure out how to change this script to make this happen. This is the structure I was thinking would help point me in the right direction, but no luck.
local tools = game.ServerStorage.Folder['foldername']:GetChildren() local treats= tools[math.random(1, #tools)]
If this is a good way to start how does this effect the rest of the script?
Thank you for taking the time to read this and for your advice!!
Your method of getting a random tool from the ServerStorage is correct, I assume you just don't know where to put it in the first code block?
Before we start, you should look into formatting your code correctly, specifically in reference to indentation. It makes everything a lot easier to look at at make changes to.
Anyway, here is how I would insert that bit into your code:
local tools = game.ServerStorage.Folder['foldername']:GetChildren() -- This never changes, so it can be set to a variable once. local debounce = true script.Parent.ClickDetector.MouseClick:connect(function(plr) if not debounce then return end -- Inverted the logic here (and removed the redundant `== true`) to removed some indentation. local treat = tools[math.random(#tools)] -- The `1, ` here was redundant if plr.Backpack:FindFirstChild(treat.Name) or plr.Character:FindFirstChild(treat.Name) then return end -- I move the debounce under this so that I could remove `debounce = true` from this if block. debounce = false treat:Clone().Parent = plr.Backpack wait(1) debounce = true end)
This code should work, although I haven't tested it myself. However, it will occasionally not give any Tool, because the randomly chosen one was already given to the Player.
I'll leave it as an exercise to figure out how to keep track of what the Player doesn't have and select only from that list. :)
Finally, I keep having to say it but there is 100% absolutely nothing wrong with using connect
over Connect
. Yes, connect
is deprecated, but that means jack. They are internally aliased to the exact same function, and will never break. If you prefer connect
, use it. If you prefer Connect
, use it instead. They are identical.
The same is true of wait
and Wait
. ROBLOX occasionally reevaluates its internal styling for consistency and these deprecations are the result of that. Any function call that differs from its non-deprecated counterpart by a capitalization change is not "truly" deprecated. These functions very rarely change functionality, if ever, and almost every time that they do the supposedly deprecated version gets the update as well.
Another set of these functions is children
, getChildren
, and GetChildren
. They all work the exact same way. Their functionality has never changed since ROBLOX has existed, and I would reckon that, like connect, they are internally aliased to the same function. That being said, only the last one, GetChildren
is not deprecated.
Case in point: ROBLOX will actually tell you if something you're using is no longer being maintained