-
What do you want to achieve?
I have a shop where a player can purchase an item. So when the player purchases an item from the shop, it fires to the server and then the server fires to the client again, allowing the item to be added to the backpack & the data is saved when the player leaves the game. -
What is the issue?
What I think is happening… is when the shop fires to the server, it doesn’t fire the information to the client. I’m trying to save the information to the DataStore, but it doesn’t save when the player buys something from the shop! -
What solutions have you tried so far?
I’ve tried re-working the serverscript that handles the RE that fires to the client, but it hasn’t been working.
I’ll insert a video showing the problem: inventory issue on Vimeo
Ignore the cake that is saved when the player rejoins — that is from a previous test! The player is supposed to have 2 cake in their inventory.
DataStore Script
local ds = game:GetService("DataStoreService")
local data = ds:GetDataStore("Tools")
local toolsFolder = game.ReplicatedStorage.Tools
local airDropInfo = require(game.ServerScriptService.AirDropInfo)
local rarities = airDropInfo["Rarities"]
local itmTemplate = game.ReplicatedStorage.ItemTemplate
game.Players.PlayerAdded:Connect(function(plr)
--[[local coins = Instance.new("NumberValue")
coins.Name = "Coins"
coins.Parent = leader
local savedCoins = data:GetAsync(plr.UserId.."-Coins")
if savedCoins then
coins.Value = savedCoins
end]]
local savedTools = data:GetAsync(plr.UserId.."-Tools")
if savedTools then
for i, item in pairs(savedTools) do
for i, tool in pairs(toolsFolder:GetChildren()) do
if item == tool.Name then
local toolClone1 = tool:Clone()
toolClone1.PickedUp.Disabled = true
toolClone1.Parent = plr.Backpack
local toolClone2 = tool:Clone()
toolClone2.PickedUp.Disabled = true
toolClone2.Parent = plr.StarterGear
local rarity = rarities[tool.Values.Rarity.Value]
local clonedTemplate = itmTemplate:Clone()
clonedTemplate.name.Text = tool.Name
clonedTemplate.image.Image = tool.TextureId
clonedTemplate.name.TextColor3 = Color3.fromRGB(255, 255, 255)
local pg = plr.PlayerGui
local InventoryMain = pg:WaitForChild("Inventory"):WaitForChild("Main")
local holder = InventoryMain:WaitForChild("Holder")
clonedTemplate.Parent = holder
end
end
end
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
plr.CharacterRemoving:Connect(function(char)
local humanoid = char.Humanoid
humanoid:UnequipTools()
end)
local suc, err = pcall(function()
local savedTools = {}
for i, item in pairs(plr.Backpack:GetChildren()) do
--if item:IsA("Tool") then
table.insert(savedTools, item.Name)
end
--end
data:SetAsync(plr.UserId.."-Tools", savedTools)
end)
if err then
warn(err)
end
end)
Shop Button
-- Reference to the button
local button = script.Parent
-- Reference to the player
local player = game.Players.LocalPlayer
local tool = game.ReplicatedStorage.Tools.Popsicle
-- Reference to the "CollectedItem" RemoteEvent
local remote = game.ReplicatedStorage.CollectedItem
local itmTemplate = game.ReplicatedStorage.ItemTemplate
function chooseReward(plr)
local toolClone1 = tool:Clone()
toolClone1.PickedUp.Disabled = true
toolClone1.Parent = plr.Backpack
local toolClone2 = tool:Clone()
toolClone2.PickedUp.Disabled = true
toolClone2.Parent = plr.StarterGear
local clonedTemplate = itmTemplate:Clone()
clonedTemplate.name.Text = tool.Name
clonedTemplate.image.Image = tool.TextureId
clonedTemplate.name.TextColor3 = Color3.fromRGB(241, 249, 240)
local pg = plr.PlayerGui
local InventoryMain = pg.Inventory.Main
local holder = InventoryMain.Holder
clonedTemplate.Parent = holder
remote:FireServer(plr, "Tool", tool, Color3.fromRGB(241, 249, 240))
end
-- Listen for the button click event
button.MouseButton1Click:Connect(function()
if player.leaderstats.Coins.Value >= 5 then
player.leaderstats.Coins.Value -= 5
script.Sound:Play()
-- Call the chooseReward function for the player
chooseReward(player)
elseif player.leaderstats.Coins.Value <= 5 then
script.Error:Play()
script.Parent.Parent.Info.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
script.Parent.Parent.Info.TextColor3 = Color3.fromRGB(253, 161, 210)
wait(.5)
script.Parent.Parent.Info.BackgroundColor3 = Color3.fromRGB(253, 161, 210)
script.Parent.Parent.Info.TextColor3 = Color3.fromRGB(255, 255, 255)
wait(.5)
script.Parent.Parent.Info.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
script.Parent.Parent.Info.TextColor3 = Color3.fromRGB(253, 161, 210)
wait(.5)
script.Parent.Parent.Info.BackgroundColor3 = Color3.fromRGB(253, 161, 210)
script.Parent.Parent.Info.TextColor3 = Color3.fromRGB(255, 255, 255)
end
end)
Backpack Script
local sg = script.Parent.Parent
local invGui = script.Parent
local mainFrame = invGui.Main
local holder = mainFrame.Holder
local itmTemplate = game.ReplicatedStorage.ItemTemplate
local dropInfo = require(script.AirDropInfo)
local rarities = dropInfo.Rarities
local plr = game.Players.LocalPlayer
local awardedFrame = script.Parent.Awarded
local awardedRemote = game.ReplicatedStorage.CollectedItem
local claim = awardedFrame.Claim
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
awardedFrame:TweenSizeAndPosition(UDim2.new(0.01,0,0.01,0), UDim2.new(0.4,0,0.35,0), "Out", "Sine", 0.4, false)
awardedFrame.Visible = false
awardedRemote.OnClientEvent:Connect(function(typ, awardedItem, rarityColor)
local award = awardedFrame:Clone()
award.Parent = awardedFrame.Parent
-- if its a weapon
if rarityColor then
award.label.Text = awardedItem.Name
award.label.TextColor3 = rarityColor
award.image.Image = awardedItem.TextureId
else
award.image.Image = "rbxassetid://4602932763"
award.label.Text = awardedItem
end
award.Visible = true
award:TweenSizeAndPosition(UDim2.new(0.2,0,0.35,0), UDim2.new(0.4,0,0.275,0), "Out", "Sine", 0.4, false)
invGui.Sound:Play()
award.Claim.MouseButton1Click:Connect(function()
award:TweenSizeAndPosition(UDim2.new(0.01,0,0.01,0), UDim2.new(0.4,0,0.35,0), "Out", "Sine", 0.4, false)
wait(0.4)
award:Destroy()
end)
end)
ServerScript that is supposed to be connected to the Shop Button & then fire to the Backpack script
local plr = game.Players.LocalPlayer
local awardedRemote = game.ReplicatedStorage.CollectedItem
awardedRemote.OnServerEvent:Connect(function(plr, request, tool, rarity)
if request == "Tool" then
print("remote connected server")
print(tool)
print(rarity)
local invGui = plr.PlayerGui.Inventory
local mainFrame = invGui.Main
local holder = mainFrame.Holder
local itmTemplate = game.ReplicatedStorage.ItemTemplate
local airDropInfo = require(game.ServerScriptService.AirDropInfo)
local dropInfo = require(game.ServerScriptService.AirDropInfo)
local rarities = airDropInfo["Rarities"]
local itmTemplate = game.ReplicatedStorage.ItemTemplate
local remote = game.ReplicatedStorage.CollectedItem
local toolClone1 = tool:Clone()
--toolClone1.PickedUp.Disabled = true
toolClone1.Parent = plr.Backpack
local toolClone2 = tool:Clone()
--toolClone2.PickedUp.Disabled = true
toolClone2.Parent = plr.StarterGear
local rarity = rarities[tool.Values.Rarity.Value]
local clonedTemplate = itmTemplate:Clone()
clonedTemplate.name.Text = tool.Name
clonedTemplate.image.Image = tool.TextureId
clonedTemplate.name.TextColor3 = rarity.color
local pg = plr.PlayerGui
local InventoryMain = pg.Inventory.Main
local holder = InventoryMain.Holder
clonedTemplate.Parent = holder
remote:FireClient(plr, "Tool", tool, rarity.color)
print("remote connected client")
end
end)
I’m more than happy to share more info if needed!