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

Saving purchases for shops?

Asked by
trecept 367 Moderation Voter
6 years ago

Is there a tutorial anywhere that can help me figure out how to do this? I want to make a shop where basically, you can buy an item, when you purchase it (and I don't know how to make it purchase with all the remote events) you keep it so when you come back to the shop it says you've bought it, and then if you click it when you own it you can equip and de-equip the item. I'm stuck on how to check if the player has the item purchased or not, without exploiters being able to get the item for free or something.

0
You could use DataStores to see if someone has bought a shop item before. UgOsMiLy 1074 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

I can help you with saving the item part of your problem.

But just a quick explanation of how you should use the RemoteEvent for the shop: So when a player wants to buy a tool, it fires the RemoteEvent to the server the item they want to buy. The server, knowing the price of the item will check the players money, see if they have enough, and if so give the item to them and subtract the price from the players money. You can even use a RemoteFunction so that a GUI will show if you dont have enough money to buy it.

But the saving part.. In the script where the RemoteEvent fires, you should set a value to a DataStore.

--at the beginning of script
local dts = game:GetService("DataStoreService")
local myStoreForShovel = dts:GetDataStore("Tools", "Shovel")--doesn't have to be a shovel, can be anything. Look up the Data Store tutorial on the Wiki to explain why I put 2 values.
local saveToStore = function(toolStore, value, player)
 toolStore:SetAsync(player.UserId, value)
end

--in you part of the remote event script if the player has enough money to buy the item
local success, errorMessage = pcall(saveToStore(myStoreForShovel, true, player)--whenever a remote event or remote function is fired, the player is the first parameter. Just rename "player" to whatever you named that parameter. This script needs the players user ID to save.
if not success then
print(errorMessage)
--if this returns true, that means the either the script had an error or the data stores are down and the purchase didn't save. You choose what to do here.
end

This probably will error and not work the best, but I just wrote this so you get a feel for it and you can write your own.

Ad

Answer this question