Hi there! ns Ggblocks20, a intermediate scripter who has actually been on the platform because that 3 year now! Today, I will certainly be make a chest that makes use of the math.random() function to provide a random item come the user who clicked the chest.


Intro

There space a couple of things we need to do prior to we have the right to start making the script and such.Lets start out by inserting a chest model right into our game:

This is a chest model I discovered in the toolbox. If you carry out grab a complimentary model chest make sure you eliminate all scripts from it.Next lets include a click detector into our chest model:

*
You deserve to name the script every little thing you want, us won’t require to call the manuscript name in bespeak to give a player an item.Now, inside of the manuscript you have actually to include this variable into the script:

local component = script.Parent--Defines the partlocal CD = Part.ClickDetector--Defines the click detector.The part var specifies the parent so we know what the parental is, climate the CD var searches through the parent to find the click detector.Next lets make our function that fires as soon as the part is clicked:

CD.MouseClick:Connect(function(Player) --Find out once the player clicks the chestprint("Event fired")end)This occasion fires when the parental is clicked, and when the event fires all the password inside of it runs. The print role isn’t needed but is an excellent to have for debugging purposes.Finally, lets add our math.random() variable:

Scripting part 2

Now that we have actually our base features made, allows make the so once math.random() is a particular number the fires an event:

if randomNum == 1 then--If math.random is equal to 1 then fire fire the eventprint("Math.random = 1")endThis code makes it so as soon as the var randomNum is equal to 1 it prints out “math.random = 1” , you should copy this to tell all numbers print out something as soon as they room chosen.Currently your code need to be somewhat similar to this:

local gui = Player.PlayerGuilocal UI1 = gui:FindFirstChild("AK-47")--Change this to everything your UI name isThe code need to be right under the MouseClicked function. This just specifies the UI and what the UI surname is, friend should adjust it to every little thing your ScreenUI surname is for example:

*
If over there are any type of bugs, or you are having actually trouble with it, please let me recognize in the replies!