Hello,
In this shop you can buy from the shop and the result should show in your inventory. It works as intended but when you leave it only saves the last item you bought. I want it to just add to your inventory. Thanks for any help.
Server Script
local ds = game:GetService("DataStoreService")
local database = ds:GetDataStore("Inventory")
local inventoryData = {}
local buyItemRM = game.ReplicatedStorage.buyItem
local updInventory = game.ReplicatedStorage.updateInventory
game.Players.PlayerAdded:Connect(function(plr)
local success = nil
local playerData = nil
local attempt = 1
repeat
success, playerData = pcall(function()
return database:GetAsync(plr.UserId) -- Assign data to player userid
end)
attempt += 1
until success or attempt == 5
if success then
if playerData then
updInventory:FireClient(plr, playerData)
else
updInventory = {
"Testing"
}
end
else
warn("DID NOT SAVE")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
if inventoryData[player.UserId] then
local success = nil
local errorMessage = nil
local attempt = 1
repeat
success, errorMessage = pcall(function()
database:SetAsync(player.UserId, inventoryData[player.UserId]) -- Assign data to player userid
end)
attempt += 1
until success or attempt == 5
if success then
print("Data Saved For".. player)
else
warn("DID NOT SAVE")
end
end
end)
buyItemRM.OnServerEvent:Connect(function(plr, characther)
print(characther)
database:SetAsync(plr.UserId, characther)
updInventory:FireClient(plr, database:GetAsync(plr.UserId))
end)
Shop Local script
local buyItemRM = game.ReplicatedStorage.buyItem
for i, v in script.Parent:GetChildren() do
if v:IsA("TextButton") then
v.Text = v:FindFirstChildWhichIsA("Model").CharacterName.Value
v.Activated:Connect(function()
for i, v in script.Parent.Parent.ViewportFrame.WorldModel:GetChildren() do
v:Destroy()
end
local itemClone = v:FindFirstChildWhichIsA("Model"):Clone()
itemClone.HumanoidRootPart.Position = Vector3.new(0,0,0)
itemClone.Parent = script.Parent.Parent.ViewportFrame.WorldModel
itemClone.HumanoidRootPart.Rotation = Vector3.new(0,0,90)
print("Ran")
print(script.Parent.Parent.ViewportFrame:GetChildren())
end)
end
end
script.Parent.Parent.Buy.Activated:Connect(function()
print(script.Parent.Parent.ViewportFrame.WorldModel:GetChildren())
buyItemRM:FireServer(script.Parent.Parent.ViewportFrame.WorldModel.Char.CharacterName.Value)
end)
Inventory Local Script
local updInventory = game.ReplicatedStorage.updateInventory
for i, v in script.Parent:GetChildren() do
if v:IsA("TextButton") then
v.Text = v:FindFirstChildWhichIsA("Model").Name -- Sets the text to the name of the model inside of the button
v.Activated:Connect(function()
for i, v in script.Parent.Parent.ViewportFrame.WorldModel:GetChildren() do
v:Destroy()
end
local itemClone = v:FindFirstChildWhichIsA("Model"):Clone()
itemClone.HumanoidRootPart.Position = Vector3.new(0,0,0)
itemClone.Parent = script.Parent.Parent.ViewportFrame.WorldModel
itemClone.HumanoidRootPart.Rotation = Vector3.new(0,0,90)
print("Ran")
print(script.Parent.Parent.ViewportFrame:GetChildren())
end)
end
end
updInventory.OnClientEvent:Connect(function(newItem)
local newTextButton = Instance.new("TextButton")
newTextButton.Parent = script.Parent
newTextButton.Text = newItem
end)