Hey guys, I am trying to make a shop GUI where you can equip and re equip the items you bought.
As well as this, I am trying to make it so the fact that you’ve bought an item can save, so after you buy an item and leave and re join the game, you can simply re equip that item.
I have no clue how to do this, so could you guys give me some help with adapting these scripts to make these things? Here are my scripts: BUY BUTTON
local replicatedStorage = game:GetService("ReplicatedStorage")
local selectedItem = script.Parent.Parent.Parent:WaitForChild("SelectedItem")
local redColor = Color3.fromRGB(255,26,26)
local greenColor = Color3.fromRGB(2,234,111)
local timeDelay = 2
local success
script.Parent.MouseButton1Click:Connect(function()
-- Remote function request
success = false
if selectedItem.Value ~= nil then
success = replicatedStorage.CheckSale:InvokeServer(selectedItem.Value)
end
if success then
script.Parent.TextColor3 = greenColor
script.Parent.Text = "Purchased!"
wait(timeDelay)
script.Parent.Text = "Buy"
else
script.Parent.TextColor3 = redColor
script.Parent.Text = "Purchase Failed!"
wait(timeDelay)
script.Parent.TextColor3 = greenColor
script.Parent.Text = "Buy"
end
end)
Shop Script
local replicatedStorage = game:GetService("ReplicatedStorage")
local shopItems = game:GetService("ServerStorage"):WaitForChild("ShopItems") -- Where tools are held
replicatedStorage.GetInfo.OnServerInvoke = function(player,item)
return shopItems[item].Coins.Value
end
replicatedStorage.CheckSale.OnServerInvoke = function(player,item)
local price = shopItems[item].Coins.Value
if player.leaderstats.Coins.Value >= price then
player.leaderstats.Coins.Value = player.leaderstats.Coins.Value - price
local gear = shopItems[item][item]:Clone()
gear.Parent = player.StarterGear
local gear = shopItems[item][item]:Clone()
gear.Parent = player.Backpack
return true
else
return false
end
end
Remotes
local replicatedStorage = game:GetService("ReplicatedStorage")
local selectedItem = script.Parent.Parent.Parent:WaitForChild("SelectedItem")
local selectedImage = script.Parent.Parent.Parent:WaitForChild("ItemDescription").SelectedImage
local selectedName = selectedImage.SelectedName
script.Parent.MouseButton1Click:Connect(function()
-- Remotefunction request
local price = replicatedStorage.GetInfo:InvokeServer(script.Parent.Name)
if price ~= nil then
selectedName.Text = price.." Coins"
selectedImage.Image = script.Parent.Image
selectedItem.Value = script.Parent.Name
end
end)
Thanks guys!
Contact me if you wanna help me - octavodad#9302
I have made a quick tutorial on your requested topic, so here we go!
The Gui
The first thing you will need is the gui, I have made a placeholder gui just for the sake of the tutorial and organized it in the workspace like this.
Coding
So first thing first just add a localscript into the gui and give it a name, in my case I named it “Main” and make a folder in ReplicatedStorage and name it Tools and add the tools to the folder. Now just add this code that I wrote to the localscript, the script is commented and has explication for every function and event.
--// Services \\--
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--// Dependencies \\--
local BG = script.Parent:WaitForChild("BG")
local Item = BG:WaitForChild("Item")
--// Variables \\--
local Player = Players.LocalPlayer
local Backpack = Player:WaitForChild("Backpack")
local Tools = ReplicatedStorage:WaitForChild("Tools")
local Items = { -- A table with the tools that you will have in the shop
"Sword"
}
--// Functions \\--
local function FindItem(ItemName)
if not Backpack:FindFirstChild(ItemName) then -- If it doesn't find the tool it will give the player the tool
local ItemClone = Tools:FindFirstChild(ItemName):Clone() -- Finds the tool by the parameter entered
ItemClone.Parent = Backpack -- It just basically gives the player the Tool itself
elseif Backpack:FindFirstChild(ItemName) then -- If it finds a tool in the player's backpack then it doesn't give it to them
print("Already Having This Item!")
end
end
--// Events \\--
Item.MouseButton1Click:Connect(function() -- On a click it activates the function
FindItem(Items[1])
end)
Saving
Basically for saving get the player’s inventory and make it into a table and on player leaving save it and just load it using a simple function like so.
local Table = {
"Table";
}
local function LoadTools(Tools, Backpack)
for _,k in pairs() do
local Tool = Tools:FindFirstChild(k)
Tool.Parent = Backpack
end
end
I understand it’s been 5 years, but the question is still relevant. I can’t find information anywhere and figure out how to save purchases. Could someone please explain in more detail about saving? Why is so little written about the most important things?