How do I fix my shop buy button script

I’m trying to make a shop Gui where the player clicks the buy button and it takes their coins in turn for some spins.

This is the code that I used.

local player = game.Players.LocalPlayer -- The player
local coins = player:WaitForChild("CoinsNum") -- IntValue for the # of coins they have
local spins = player:WaitForChild("SpinsNum") --IntValue for the # of spins they have
local buyButton = script.Parent -- The actual buy button
local cost = script.Parent.Parent.Cost -- The cost button

buyButton.MouseButton1Click:Connect(function() -- When the player clicks the buy button
	local ex = string.lower(tostring(cost.Text)) -- Detects the text of the cost button
	if ex:find("Cost: 10") then -- If the text is ("blank") then do whatever next
			if coins.Value >= 10 then -- If the players CoinsValue is Greater Than or Equal To 10 then
				spins.Value = spins.Value + 1 -- Give the player 1 spin
				coins.Value = coins.Value - 10 -- Take away 10 Coins
			else print("Not enough money!") -- If they don't have enough then print this
			end 
		elseif ex:find("Cost: 100") then
			if coins.Value >= 100 then
				spins.Value = spins.Value + 10
				coins.Value = coins.Value - 100
			else print("Not enough money!")
			end

		elseif ex:find("Cost: 250") then
			if coins.Value >= 250 then
				spins.Value = spins.Value + 25
				coins.Value = coins.Value - 250
			else print("Not enough money!")
			end
		elseif ex:find("Cost: 500") then
			if coins.Value >= 500 then
				spins.Value = spins.Value + 50
				coins.Value = coins.Value - 500
			else print("Not enough money!")
			end
		elseif	ex:find("Cost: 1000") then
			if coins.Value >= 1000 then
				spins.Value = spins.Value + 100
				coins.Value = coins.Value - 1000
			else print("Not enough money!")
			end
		elseif ex:find("Cost: 2500") then
			if coins.Value >= 2500 then
				spins.Value = spins.Value + 250
				coins.Value = coins.Value - 2500
			else print("Not enough money!")
			end
	else print("what are u trying to buy it aint even detect it lol") -- If none of those texts are on the cost button then print this
		end
	end)

To avoid using multiple buy buttons for every single thing the player can buy I thought I’d use one, that’s why I detect if the cost text is equal to a specific text.

Whenever I actually test it out it always prints the “what are you even trying to buy” and I don’t know why.

Maybe I missed something or maybe I messed something up but any help would be greatly appreciated!

4 Likes

This is not really an efficient way since this can be easily exploited on the client.

1 Like

Since luau is Capitalization sensitive:
“Cost 10” ~= “cost 10”

2 Likes

I’ve already found a couple errors. For starters, you’re using a capital C in the if statements, when you’ve made the string completely lowercase. Next, you’re expecting them to type in “Cost: __”, which is obviously not the best for UX.
What you could do is add a TextLabel to show the cost, and have an up and down button to let the player choose from a list of prices they could pay.

2 Likes

I do not intend for the game to ever get released this is just a thing for me and my friends since I like creating things.

1 Like

What i would definitely recommend doing is making a module script and putting your spins and the costs of those spins inside a table then looping through that table.

1 Like

The players aren’t allowed to set the price, they are all set prices here’s a screenshot.

That’s not what I meant. I said “choose from a list of prices”, not define their own amount.

ModuleScripts are too early for this person in particular, judging from their code. But for this kind of stuff, it would be useful to use a ModuleScript, so I suggest OP should learn the basics. If they don’t get it the first try, it’s fine. I’ve never done anything new first try.

2 Likes

It’s true, I am in no shape or form a scripter whatsoever. I only know pretty basic stuff, I’m more of a builder/animator, but the urge to make my own game really hit me so I tried, I’ll try doing what you guys are saying and if anything works I’ll come back and let you guys know if it doesn’t Ill try and learn more until something does work. Thank you guys for the help!

2 Likes

Don’t be so harsh on yourself, I understand, It took me a whole 4 years to gain experience and get to where i am today. I always used to say i couldn’t do anything but learning, understanding and being patient really did a lot to my programming skills.

2 Likes