Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

"Random" table picker only having out output?

The goal is when a player presses a button, a random option is chosen and it runs some options according to which one is picked. The issue is, i only ever get the first number, the second two numbers never get chosen. Anyone know why(also, anyway i can optimize it?) Thanks

buyButton = script.Parent
funds = script.Parent.Parent.Parent.Funds
local Skins =
{633409829,
633412461,
633490575,
}

buyButton.MouseButton1Click:connect(function()
	local x = Skins[math.random(1,#Skins)]
	if x == 633409829 and funds.Value > 24 then
		buyButton.Parent.Cancel.Visible = false
		buyButton.Parent.TextLabel.Text = "Processing"
		wait(3)
		buyButton.Parent.TextLabel.Text = "Blossom Skin"
		script.Parent.Parent.Image.Image = "rbxassetid://633409829"
		script.Parent.Parent.Parent.Funds.Value = script.Parent.Parent.Parent.Funds.Value - 25
		script.Parent.Parent.Parent.Parent.Skins.blossom.Value = true
		script.Parent.Text = "Accept"
	if x == 633412461 and funds.Value > 24 then
		buyButton.Parent.Cancel.Visible = false
		buyButton.Parent.TextLabel.Text = "Processing"
		wait(3)
		buyButton.Parent.TextLabel.Text = "Desert Camo"
		script.Parent.Parent.Image.Image = "rbxassetid://633412461"
		script.Parent.Parent.Parent.Funds.Value = script.Parent.Parent.Parent.Funds.Value - 25
		script.Parent.Parent.Parent.Parent.Skins.desert.Value = true
		script.Parent.Text = "Accept"
	if x == 633490575 and funds.Value > 24 then
		buyButton.Parent.Cancel.Visible = false
		buyButton.Parent.TextLabel.Text = "Processing"
		wait(3)
		buyButton.Parent.TextLabel.Text = "Carbon Fiber"
		script.Parent.Parent.Image.Image = "rbxassetid://633490575"
		script.Parent.Parent.Parent.Funds.Value = script.Parent.Parent.Parent.Funds.Value - 25
		script.Parent.Parent.Parent.Parent.Skins.carbon.Value = true
		script.Parent.Text = "Accept"
		end
		end
	end
end)

2 comments

2

Try replacing the 'if's after the first if x == blahblah with elseif

0

The way I see it, is that you have to replace the "if"'s after the first "if" with "elseif" ... also I believe you have two too many "end"'s

1 answer

✓ Accepted answer

Firstly please use some more variables as you access each the full path for each item. The main problem is the way you have your if statements ie:-

-- badly formated code
if [logic] then
-- code
if [logic] then
-- code
if [logic] then
-- code
end
end
end

-- formatted code
if [logic] then
-- code
	if [logic] then
	-- code
		if [logic] then
		-- code
		end
	end
end

You can see how only your first statement has to be true for the others to run.

Lastly you have a lot of duplicate code and there are better ways of doing this so we only use the same logic but with different information, we could use a table to store our data in:-

Example:-

-- we creat variables so we do not need to access the full path each time
local buyButton = script.Parent
local funds = script.Parent.Parent.Parent.Funds
local cancel = buyButton.Parent.Cancel
local statusTxt = buyButton.Parent.TextLabel
local img = script.Parent.Parent.Image
local skins = script.Parent.Parent.Parent.Parent.Skins
local txt = script.Parent

-- this is our data list so we do not use lots of logic just for checking the same thing
local data = {
	{
		name = 'Blossom Skin',
		img = 'rbxassetid://633409829',
		skinName = 'blossom',
		cost = 24
	},
	{
		name = 'Desert Camo',
		img = 'rbxassetid://633412461',
		skinName = 'desert',
		cost = 24	
	},
	{
		name = 'Carbon Fiber',
		img = 'rbxassetid://633490575',
		skinName = 'carbon',
		cost = 24		
	}
}

local deb = false -- debounce
buyButton.MouseButton1Click:connect(function()
	if deb then return end
	deb = true
	
	local itmData = data[math.random(1,#data)]
	if funds.Value >= itmData.cost then -- check price
		-- proces this random item
		cancel.Visible = false
		statusTxt.Text = 'Processing'
		wait(3)
		if funds.Value >= itmData.cost then -- we have waited so we need to check this value again as it may change
			statusTxt.Text = itmData.name
			img.Image = itmData.img
			funds.Value = funds.Value - itmData.cost
			local skin = skins:FindFirstChild(itmData.skinName)
			if skin then skin.Value = true end -- enable skin if found
			txt.Text = 'Accept'
		end
	end
	
	deb = false
end)

I hope this helps please comment if you do not understand how / why this script works.

2 comments

0

Thank you, this also makes it easier to add skins! :D

0

i get an error that does not occur in studio, only in game Skins is not a valid member of screen gui

This discussion is preserved as it was. New answers, comments, and votes are closed.