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

How to make the Random Item give just one item each time?

Asked by 6 years ago

Hello! I have a working random item giver in my shop. The only problem is it sometimes gives more than one item, and charges the player multiple times, when it should only be one click = one item. But it doesn't always do this, sometimes it works fine.

My random item giver also does not give duplicates. When the player presses the Buy button the script checks if the player already has a Bool Value that represents the item, if the player does not have the item the Remote fires the server and grants the item while removing the amount. And if the player already has the item, the description reads out a message saying as such and no charge is done.

How can I make it to where the script doesn't attempt to charge multiple times when the player presses the Buy button??

Script below:

local Item1Coinsbutton = script.Parent.Frame.CRATESFrame.Item1Frame.BuyCoins -- The Buy button.
script.Parent.Frame.CRATESFrame.Item1Frame.CoinsPrice.Text =100

Item1Coinsbutton.MouseButton1Click:Connect(function()
local amount =100
local descText = script.Parent.Frame.CRATESFrame.Item1Frame.DescriptionFrame.ItemDescription

local random = math.random(1,200)

if Player.leaderstats.Coins.Value >= amount then

--Item 1, checks to see if the player does not own the item, and then grants.
if random >0 and random <10 and Player.Inventory1.HasGunBlue.Value == false then
CoinsPurchase:FireServer(Player.Inventory1.HasGunBlue, amount)
descText.Text = 'You received Gun! Check your Blue Inventory!'
    wait(3)

--Checks to see if the player does own Item 1, if they do, the description box will say so.
elseif random >0 and random <10 and Player.Inventory1.HasGunBlue.Value == true then
descText.Text = 'You received Gun Blue! You already own this item. Try again!'
    wait(3)

--Item 2 checks to see if the player does not own, and then grants.
elseif random >11 and random <20 and Player.Inventory1.HasGunGreen.Value == false then
CoinsPurchase:FireServer(Player.Inventory1.HasGunGreen, amount)
descText.Text = 'You received Gun! Check your Blue Inventory!'
    wait(3)

--Checks to see if the player does own Item 2, if they do, the description box will say so.
elseif random >11 and random <20 and Player.Inventory1.HasGunGreen.Value == true then
descText.Text = 'You received Gun Green! You already own this item. Try again!'
    wait(3)

--Item3-20, etc.
end
end

wait(5)
Item1Frame.Visible = false
end)
end)
0
uhh. chatiii 6 — 6y

1 answer

Log in to vote
0
Answered by
Jellyfosh 125
6 years ago

Please name your variables! It make scripts a lot easier to read AND write! You can avoid a function performing its operation more times that wanted using a debounce. Basically you set a variable to false. When a function is activated, check to see if debounce is false, if it is, set it to true, and when your function is finished set it back to false.

I have a feeling that not having a debounce isn't exactly your problem, though. While I was unable to find exactly what's causing your error to happen, I suspect it to be disorganization and putting your "end"s in the wrong spots. Also, if "random" can only be less than, for example, 10 and greater than 11, your script will not do anything if random is 10 or 11. You'll want to use greater than or equal to (>=) and less than or equal to (<=).

I tried cleaning your code a little bit, try this out:

local Item1Coinsbutton = script.Parent.Frame.CRATESFrame.Item1Frame.BuyCoins -- The Buy button.
script.Parent.Frame.CRATESFrame.Item1Frame.CoinsPrice.Text =100

Item1Coinsbutton.MouseButton1Click:Connect(function()
local amount =100
local descText = script.Parent.Frame.CRATESFrame.Item1Frame.DescriptionFrame.ItemDescription

local random = math.random(1,200)

if Player.leaderstats.Coins.Value >= amount then
    if random >= 1 and random <= 10 then 
        local blueGun = Player.Inventory1.HasGunBlue
        if blueGun.Value == false then
            CoinsPurchase:FireServer(blueGun, amount)
            descText.Text = 'You received Gun! Check your Blue Inventory!'
        elseif blueGun.Value == true then
            descText.Text = 'You received Gun Blue! You already own this item. Try again!'
        end --this ends your call to see if bluegun is true or false
    wait(3)
    elseif random >= 11 and random <=20 then
        local greenGun = Player.Inventory1.HasGunGreen
        if greenGun.Value == false then
            CoinsPurchase:FireServer(greenGun, amount)
            descText.Text = 'You received Gun! Check your Blue Inventory!'
        elseif greengun.Value == true then
            descText.Text = 'You received Gun Green! You already own this item. Try again!'
        end --This ends your call to see if greengun is true or false
    wait(3)
        --You would put more "elsif" statements here to check the "random"
    end --This ends your checks on "random" (if random >= 1 and random <= 10 then)
end --This ends "if Player.leaderstats.Coins.Value >= amount then"


wait(5)
Item1Frame.Visible = false
end) --This ends your MouseButton1Click


0
I appreciate the response! I tried it your way, but it's still randomly charging/granting more than once per button press. =/ Any thoughts? Never2Humble 90 — 6y
0
I added a debounce too, no difference. =/ It works just fine the first few times the button is pressed and the transaction happens, then it randomly doesn't, Never2Humble 90 — 6y
Ad

Answer this question