When i buy something i want instead of “Buy now”, “Equip now” and it’s not able to re-buy it. sorry for my broken english
like this:
When i buy something i want instead of “Buy now”, “Equip now” and it’s not able to re-buy it. sorry for my broken english
like this:
You would somehow need to know the purchases of the player. U can do this by inserting a folder into the player and adding something into the folder everytime the player purchases something, or by creating a module that stores it.
When an item is selected, you need to check if the player already owns the item (and an equip button needs to be displayed), or if they do NOT own it, and to display a purchase button.
You could first insert a script into that TextButton/ImageButton. Then the script would go something like this:
local Button = script.Parent --The parent of the script, which is the TextButton.
Button.MouseButton1Click:Connect(function() --Detect when a player clicks the box.
if Label.Text == "Buy Now" then --If the button displays "Buy Now"
Label.Text = "Equipped"
elseif Label.Text == "Equipped" then --If the button displays "Equipped".
Label.Text = "Equip"
elseif Label.Text == "Equip" then --If the player owns it but it isn't equipped.
Label.Text = "Equipped"
end
end)
However if you were wondering how to save the player’s purchase, then you could use DataStores. So you would need to use a RemoveEvent to retrieve information from a DataStore, then send it to the local client. From there, check if the player owns the item. For the server side, you could save their purchase like:
local DSS = game:GetService("DataStoreService")
local PurchaseSaves = DSS:GetDataStore("Purchases")
-- Do something when they buy it.
PurchaseSaves:SetAsync("Sword - "..player.UserId,1) -- Given that you have already defined the player, and "Sword" would be the name of the item they are purchasing.
Then to check if they have purchased it, you can do something like:
if PurchaseSaves:GetAsync("Sword - ") == 1 then
--They have the item, do what you must.
end
Then from there you would let the client know that, and use the script at the very top to check what the status of the button is. Or if you meant something else, please let us know.
Edit: Thanks to @LukaDev_0 for pointing out an error I made.
Its :SetAsync not :SaveAsync. And you should store the purchases in a table not in individual keys
Oh my bad, I don’t know why I wrote :SaveAsync. However individual keys would be easier to retrieve and remember no?
Saving the data when the player purchases it can result in Data Loss because of rate limits. Saving all the data in one key when the player leaves in on big table is faster since its only one key.
DataStores cannot be accessed via LocalScripts, so you would need to retrieve from the DataStore using a ServerScript and a RemoveEvent. You can refer to this documentation for more information.
this isn’t a local script. i put this script in serverscriptservice.
Then “LocalPlayer” should not be used. To register the player joining using a ServerScript, do something like:
local DSS = game:GetService("DataStoreService")
local PurchaseSaves = DSS:GetDataStore("Purchases")
game.Players.PlayerAdded:Connect(function(player)
if PurchaseSaves:GetAsync("Yellow Breath - "..player.UserId) == 1 then
--They have the item, do stuff. Something like the line of code below, but add more.
player:WaitForChild("PlayerGui"):WaitForChild("ShopGui").Frame.TextLabel.Text = "Equip"
else
--They do not have the item, do stuff.
player:WaitForChild("PlayerGui"):WaitForChild("ShopGui").Frame.TextLabel.Text = "Buy now"
end
end)
Something along these lines.
Please remember that scripting support is not the place to ask for code nor expect it to come for free. You must attempt if yourself first and come back when you run into issues (show us what you have so far).
Sorry, I also forgot to mention that you should use pcall()
s when updating DataStores, as they may error from time to time. For the line of code that you will be updating or setting their data with, wrap it in a pcall()
like this:
local yes,no = pcall(function() DATA_STORE_NAME:SetAsync( --[[ The data you are updating ]] )
if yes then
--Give them the item or something.
elseif no then
--Let them know an error has occured.
end
This is so that the player will not be confused if they did not receive their item.
it’s my fifth day at scripting. the only way I KNOW to learn scripting is using the forum. i’m here to ask something and trying to learn what they are saying.
You should learn the basics before trying to create anything sensitive such as a shop. Why sensitive? Shops deal with the official currency. Unless your shop isn’t associated with gamepasses or developer products, you may end up looking like scamming players.
Have a remote event firing to the server when ever someone clicks the “Buy” button. Check if they have enough currency to buy the item on server. If they do have enough then make them buy it and return true. On the local script check if return is true and if it is then the purchase is successful.