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

Need help with a shop script please?

I have a shop script that uses a crafting system, but in a way my shop is unfair. Let me explain:

EX: Jimmy wants to try to buy a sword, he has 3 mushrooms but not 2 cows, when he presses the button, all the required amount of Jimmy's mushrooms are gone and the transaction is not made.

local items = script.Parent.Frame:GetChildren()

for i = 1, #items do
function buy()
	local item1 = items[i]:FindFirstChild("Item1")
	local item2 = items[i]:FindFirstChild("Item2")
	local item3 = items[i]:FindFirstChild("Item3")
	local item4 = items[i]:FindFirstChild("Item4")
	local item5 = items[i]:FindFirstChild("Item5")
	local Cash = script.Parent.Parent.Parent.Parent.leaderstats:FindFirstChild("Gold")
	if item1 then
		if script.Parent.Parent.Parent.Parent.Materials:FindFirstChild(item1.Value).Value > items	[i].item1Price.Value then
			script.Parent.Parent.Parent.Parent.Materials:FindFirstChild(item1.Value).Value = 	script.Parent.Parent.Parent.Parent.Materials:FindFirstChild(item1.Value).Value - items	[i].item1Price.Value
			else return
		end
	end
	if item2 then
		if script.Parent.Parent.Parent.Parent.Materials:FindFirstChild(item2.Value).Value > items	[i].item2Price.Value then
			script.Parent.Parent.Parent.Parent.Materials:FindFirstChild(item2.Value).Value = 	script.Parent.Parent.Parent.Parent.Materials:FindFirstChild(item2.Value).Value - items	[i].item2Price.Value
			else return
		end
	end
	if item3 then
		if script.Parent.Parent.Parent.Parent.Materials:FindFirstChild(item3.Value).Value > items	[i].item3Price.Value then
			script.Parent.Parent.Parent.Parent.Materials:FindFirstChild(item3.Value).Value = 	script.Parent.Parent.Parent.Parent.Materials:FindFirstChild(item3.Value).Value - items	[i].item3Price.Value
			else return
		end
	end
	if item4 then
		if script.Parent.Parent.Parent.Parent.Materials:FindFirstChild(item4.Value).Value > items	[i].item4Price.Value then
			script.Parent.Parent.Parent.Parent.Materials:FindFirstChild(item4.Value).Value = 	script.Parent.Parent.Parent.Parent.Materials:FindFirstChild(item4.Value).Value - items	[i].item4Price.Value
			else return
		end
	end
	if item5 then
		if script.Parent.Parent.Parent.Parent.Materials:FindFirstChild(item5.Value).Value > items	[i].item5Price.Value then
			script.Parent.Parent.Parent.Parent.Materials:FindFirstChild(item5.Value).Value = 	script.Parent.Parent.Parent.Parent.Materials:FindFirstChild(item5.Value).Value - items	[i].item5Price.Value
			else return
		end
	end
	if Cash.Value >= items[i].Cost.Value then
		Cash.Value = Cash.Value - items[i].Cost.Value
		local tool1 = game.ReplicatedStorage:FindFirstChild	(script.Parent.Folder.Value):FindFirstChild(items[i].ItemName.Value):Clone()
		local tool2 = game.ReplicatedStorage:FindFirstChild	(script.Parent.Folder.Value):FindFirstChild(items[i].ItemName.Value):Clone()
		tool1.Parent = script.Parent.Parent.Parent.Parent.Backpack
		tool2.Parent = script.Parent.Parent.Parent.Parent.StarterGear
		print("Purchase successful.")
	end
items[i].BuyButton.MouseButton1Click:connect(buy)
end
end

no errors appear in the output or script analysis

1 answer

Combine your conditions into one

Instead of using a sequence of if statements, and them things going horribly wrong when one of them doesn't say yes, evaluate all of your conditions at once using and

if item1 and item2 and item3 and item4 and item5 then
-- Has them all
end

2 comments

0

but not all weapons have 5 items for crafting.

0

Then make it suitable? Your issue isn't that not all weapons need 5, it's that when you only have some it removes what you have and escapes. My solution fixes that issue.

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