Purchase Script Won't Work

I was trying to make a purchase button for the game I am creating. When you press a purchase button, it fires a remote event in a folder which is located in ReplicatedStorage called “BuyEvent.” When it fires it, it sends pie(current pie they are viewing), and plr(the player). Does anyone know what is happening here? Code:

local rs = game:GetService("ReplicatedStorage")
rs.Remotes.BuyEvent.OnServerEvent:Connect(function(plr, pie)
print("3")

if pie == "Pumpkin" and plr.OwnedPies.Pumpkin.Value == false then
	if plr.leaderstats.Coins.Value <= 0 then
		plr.leaderstats.Coins.Value = plr.leaderstas.Coins.Value - 0
		plr.OwnedPies.Pumpkin.Value = true
		print("4")
	end
	end
		
		
	if pie == "Cherry" and plr.leaderstats.Coins.Value <= 10 and plr.OwnedPies.Cherry.Value == false then
		plr.leaderstats.Coins.Value = plr.leaderstas.Coins.Value - 10
		plr.OwnedPies.Cherry.Value = true
		print("5")
		end
		
		if pie == "Blueberry" and plr.leaderstats.Coins.Value <= 20 and plr.OwnedPies.Blueberry.Value == false then
		plr.leaderstats.Coins.Value = plr.leaderstas.Coins.Value - 20
		plr.OwnedPies.Blueberry.Value = true
		print("6")
		end
		
			if pie == "Apple" and plr.leaderstats.Coins.Value <= 30 and plr.OwnedPies.Apple.Value == false then
		plr.leaderstats.Coins.Value = plr.leaderstas.Coins.Value - 30
		plr.OwnedPies.Apple.Value = true
		print("7")
		end
end)
3 Likes

rs is not defined.

1 Like

I forget to put that in the post. It was defined earlier in the script. Sorry.

2 Likes

Are there any errors in game in the dev console?

1 Like

For the pumpkin, you’re checking if the player has negative amount of coins before giving them the pumpkin, which doesn’t make any sense. You want the player to be able to buy it if they have just enough (or more) coins.

Same with the other ones, you’re checking if the player has less than or equal to the price. Why would you want the player to buy something if they don’t have enough money?

You should be doing

if plr.leaderstats.Coins.Value >= Price then
1 Like

Does the console and output show the same thing?

1 Like

Yes. The output is just not available if you go in game.

1 Like

No errors.

1 Like

Ok, read what Spynaz said. I don’t see any other errors, assuming everything is defined.

1 Like

I there are no errors, chances are, the code that you want to be running, is not. Your if statements are flawed. @Spynaz is correct. Nothing will get past those if statement blocks unless the name is the same AND their money is less(?) than the price AND if they don’t already own it.

General tip: If there are no errors and the script is enabled, the code is running but the logic within the script is somehow flawed. Learn to use breakpoints and visualise how your code is running.

3 Likes

After that, it still doesn’t work.

1 Like

You also have have a spelling mistake. Each line where you update the player’s coin balance, you wrote “leaderstas” instead of “leaderstats”. Example from your code:

plr.leaderstats.Coins.Value = plr.leaderstas.Coins.Value - 20

This is why its good to have the output open when testing, as it will show any errors (such as this one) that occur which will help you solve the issue.

1 Like

Thank you for telling me about that. I do look in the output and for some reason it didn’t show that. The script still won’t work. I don’t know if I mentioned this, but it only made it to print(“3”).

1 Like

Is pie an instance or a string?

If you are comparing an instance to a string (“Pumpkin”) the If statement will return false, hence the code wouldn’t let you get past that first if statement.

Try using pie.Name if pie is an instance.

1 Like

As @wevetments said, it may be because you are sending the actual object rather than the name. I’m guessing thats the case since you said

So in this case you should be sending the name of the pie

2 Likes

Pie is a string. The script that fires the event has this:

local pie = script.Parent.Shop.SideBar.Pie.Value
1 Like

Where is this OwnedPies and Pumpkin created? The server or client?

1 Like

OwnedPies is created by a script in ServerScriptService in the player. OwnedPies is a folder and Pumpkin is a Bool Value in OwnedPies.

When testing the code myself I was met with zero errors. Other then the typo in the code provided (which someone already pointed out). Everything else worked flawlessly.

In the Server Event function. pie was a string and it modified the bool values in ‘OwnedPies’ while also deducing the amount of coins that was defined in the script.
I made very minor edits to the code. I’ll provide the file I used below if you wish to take a look at it.
Pies.rbxl (558.7 KB)

@Linked_S
Have to correct you on that, you can actually use the output in game, if you press F9 it opens the dev console showing Client output and if you’re the place owner it will show server output as well and you can even run commands.