W ReplicatedStorage dodaj itemy, które będzie można kupić oraz Remote Event o nazwie, np. ShopRE
Dodaj obiekt sklepu
Dodaj obiekt podrzedny, np. ShopNPC z ProximityPrompt i skryptem Shop
local prompt = script.Parent.ProximityPrompt
local shopRE = game.ReplicatedStorage.ShopRE
local shopItems = game.ReplicatedStorage.ShopItems
prompt.Triggered:Connect(function(player)
shopRE:FireClient(player)
end)
shopRE.OnServerEvent:Connect(function(player, itemName, price)
local item = shopItems:WaitForChild(itemName)
if price < 0 then
return
end
if item then
local itemClone = item:Clone()
itemClone.Parent = player.Backpack
player.leaderstats.Coins.Value -= price
end
end)
Dodaj ScreenGUI w StarterGUI oraz Frame o nazwie np. ShopFrame
Wewnątrz ShopFrame dodaj skrypt lokalny o naziwe, np. ShopLocal
local shopRE = game.ReplicatedStorage:WaitForChild("ShopRE")
local shopFrame = script.Parent
local player = game.Players.LocalPlayer
local item = shopFrame.Item
shopRE.OnClientEvent:Connect(function()
shopFrame.Visible = true
end)
local function CloseShopMenu()
shopFrame.Visible = false
end
shopFrame.ExitBtn.MouseButton1Click:Connect(CloseShopMenu)
player.Character:WaitForChild("Humanoid").Running:Connect(CloseShopMenu)
shopFrame.ItemTextInfo.Text = item.ItemName.Value.."\n".."Price: "..item.Price.Value
shopFrame.BuyBtn.MouseButton1Click:Connect(function()
if player.leaderstats.Coins.Value < item.Price.Value then
print("not enough coins")
return
end
local char = player.Character or player.CharacterAdded:Wait()
local backpack = player.Backpack
if char:FindFirstChild(item.ItemName.Value) or backpack:FindFirstChild(item.ItemName.Value) then
print("you have got this item")
return
end
shopRE:FireServer(item.ItemName.Value, item.Price.Value)
end)
Sklep z wieloma produktami
Każdy item GUI w folderze ShopItems ma na sobie 2 atrybuty: Damage i Price.
Item nazywa się dokładnie tak jak jego odpowiednik Tool w folderze ShopItems w Replicated Storage
Skrypt ShopLocal odpowiadający za wyświetlanie GUI i wybór itemka do kupienia
local shopRE = game.ReplicatedStorage:WaitForChild("ShopRE")
local shopFrame = script.Parent
local player = game.Players.LocalPlayer
local items = shopFrame.ShopItems:GetChildren()
local item = nil
for k,v in pairs(items) do
v.Activated:Connect(function()
item = {
itemName = v.Name,
damage = v:GetAttribute("Damage"),
price = v:GetAttribute("Price")
}
shopFrame.ItemTextInfo.Text =
item.itemName.."\n"..
"Price: "..item.price.."\n"..
"Damage: "..item.damage
end)
end
shopRE.OnClientEvent:Connect(function()
shopFrame.Visible = true
end)
local function CloseShopMenu()
shopFrame.Visible = false
end
shopFrame.ExitBtn.MouseButton1Click:Connect(CloseShopMenu)
player.Character:WaitForChild("Humanoid").Running:Connect(CloseShopMenu)
shopFrame.BuyBtn.MouseButton1Click:Connect(function()
if not item then return end
if player.leaderstats.Coins.Value < item.price then
print("not enough coins")
return
end
local char = player.Character or player.CharacterAdded:Wait()
local backpack = player.Backpack
if char:FindFirstChild(item.itemName) or backpack:FindFirstChild(item.itemName) then
print("you have got this item")
return
end
shopRE:FireServer(item.itemName, item.price)
end)
Dodanie możliwości zapisu kupionych produktów w DataStore
Dodajemy BindableEvent o nazwie SaveItemsBE w ServerStorage
Skrypt Shop dołączony do sklepu / NPC, który umożliwi zakup wybranego wcześniej produktu
local prompt = script.Parent.ProximityPrompt
local shopRE = game.ReplicatedStorage.ShopRE
local shopItems = game.ReplicatedStorage.ShopItems
prompt.Triggered:Connect(function(player)
shopRE:FireClient(player)
end)
shopRE.OnServerEvent:Connect(function(player, itemName, price)
local item = shopItems:WaitForChild(itemName)
if price < 0 then
return
end
if item then
local itemClone = item:Clone()
itemClone.Parent = player.Backpack
player.leaderstats.Coins.Value -= price
-- saving item
local saveItemsBE = game.ServerStorage.SaveItemsBE
saveItemsBE:Fire(player, itemName)
end
end)
Skrypt ItemsManager w ServerScriptService, który pozwala zapisać i załadować zakupione itemy po ponownym dołączeniu do gry
local dataStoreService = game:GetService("DataStoreService")
local store = dataStoreService:GetDataStore("ItemStore")
local items = game.ReplicatedStorage:WaitForChild("ShopItems")
local saveItemsBE = game.ServerStorage.SaveItemBE
local data
local function SaveData(player, data)
store:SetAsync(player.UserId, data)
end
local function InitializeData(player)
data = nil
local success, msg = pcall(function()
data = store:GetAsync(player.UserId)
end)
if not success then
print("Data failure", msg)
end
if not data then
data = {}
print("First time")
SaveData(player,data)
end
if data then
for k,v in pairs(data) do
local item = items:FindFirstChild(data[k])
print(v)
local newItem = item:Clone()
newItem.Parent = player.Backpack
end
end
end
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function()
InitializeData(player)
saveItemsBE.Event:Connect(function(player, itemName)
table.insert(data, itemName)
SaveData(player,data)
end)
end)
--SaveData(player, false)
end)