Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Using math.random with a folder in ServerStorage?

Asked by 6 years ago
Edited 6 years ago

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!!

0
connect is deprecated, use Connect Thundermaker300 554 — 6y
0
wait() is deprecated, use Wait() hiimgoodpack 2009 — 6y
0
Hi, thank you! SeeMoreHearts 12 — 6y
0
@hiimgoodpack, Roblox Studio is telling me that Wait() is deprecated and not wait(). I corrected my Connect typo. Are my variables structured correctly? Thanks, again! SeeMoreHearts 12 — 6y

1 answer

Log in to vote
0
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
6 years ago
Edited 6 years ago

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

0
You are Amazing! :D Seriously, want to cry with relief, because I became frustrated trying to figure out how to do this for days! I could extract a tool from the ServerStorage, but not a random tool from a folder. SeeMoreHearts 12 — 6y
0
It did give me a "Folder isn't a vallid member of ServerStorage" (paraphrasing), so I removed the folder name in brackets and then it worked! :D SeeMoreHearts 12 — 6y
0
The brackets weren't the problem there, probably had a typo you didn't notice. :P adark 5487 — 6y
0
That is to say, thing.child == thing["child"] in Lua adark 5487 — 6y
Ad

Answer this question