What solutions have you tried so far? Did you look for solutions on the Developer Hub?
Dev fourms and looking online. i have tried placing my print statements to see what went wrong
local script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Items = ReplicatedStorage:WaitForChild("Gears"):GetChildren()
local Container = script.Parent.Container
local itemFrame = Container.ItemsFrame
local shopUI = script.Parent
local exit = shopUI.Container.Exit
local player = Players.LocalPlayer
local PurchaseSound = shopUI.Sounds.Purchase
local ErrorSound = shopUI.Sounds.Error
function updateItems()
for i, item in pairs(Items) do
-- Find any old buttons
local oldButton = itemFrame:FindFirstChild(item.Name)
if oldButton then
oldButton:Destroy()
end
local newButton = itemFrame.TemplateButton:Clone()
newButton.Name = item.Name
newButton.TowerName.Text = item.Name
newButton.Image = item.Config.Image.Texture
newButton.LayoutOrder = item.Config.Price.Value
newButton.Parent = itemFrame
newButton.Visible = true
newButton.Activated:Connect(function()
local coinLeaderstats = player:WaitForChild("leaderstats").Coins
if coinLeaderstats.Value >= item.Config.Price.Value then
--coinLeaderstats.Value -= item.Config.Price.Value
local remoteItem = item.Config.Price
ReplicatedStorage.Remotes.SaveData:FireServer(remoteItem)
if item then
local newitem = item:Clone()
ReplicatedStorage.Remotes.ShopItemGiver:FireServer(player, newitem)
else
warn("erteyertert")
end
PurchaseSound:Play()
else
-- error
warn("Not enough coins!")
ErrorSound:Play()
end
end)
end
end
local function toggleShop()
shopUI.Container.Visible = not shopUI.Container.Visible
if shopUI.Container.Visible then
updateItems()
end
end
local function setupShop()
local prompt = Instance.new("ProximityPrompt")
prompt.RequiresLineOfSight = true
prompt.ActionText = "Shop"
if workspace.Shop:WaitForChild("ShopPart") then
prompt.Parent = workspace.Shop:FindFirstChild("ShopPart")
end
prompt.Triggered:Connect(toggleShop)
exit.Activated:Connect(toggleShop)
end
setupShop()
Script
local replicatedStorage = game:GetService("ReplicatedStorage")
local event = replicatedStorage.Remotes.ShopItemGiver
event.OnServerEvent:Connect(function(player, newitem)
if newitem and newitem ~= nil then
local Backpack = player:WaitForChild("Backpack", 30)
print(newitem)
newitem.Parent = Backpack
else
warn("No Item")
end
end)
Its because you’re trying to parent the player to their own backpack.
This is happening because when you are firing the remote to the server, you are passing the player as an argument
when firing remotes to the server the player is automatically passed as the first argument so there is no need for you to pass the player. Instead, change that line to this:
ReplicatedStorage.Remotes.ShopItemGiver:FireServer(newitem) -- No player
That is because you cloned the item locally, and since it is on the players local machine the server can not see it. Instead pass the item you want to be cloned and then clone it on the server.
Hey there. sorry for not replying. I cloned it on the server and it all works now! thanks alot
Server Code:
local replicatedStorage = game:GetService("ReplicatedStorage")
local event = replicatedStorage.Remotes.ShopItemGiver
event.OnServerEvent:Connect(function(player, item)
if item and item ~= nil then
local Backpack = player:WaitForChild("Backpack", 30)
print(item)
local newItem = item:Clone()
newItem.Parent = Backpack
else
warn("No Item")
end
end)
client code:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Items = ReplicatedStorage:WaitForChild("Gears"):GetChildren()
local Container = script.Parent.Container
local itemFrame = Container.ItemsFrame
local shopUI = script.Parent
local exit = shopUI.Container.Exit
local player = Players.LocalPlayer
local PurchaseSound = shopUI.Sounds.Purchase
local ErrorSound = shopUI.Sounds.Error
function updateItems()
for i, item in pairs(Items) do
-- Find any old buttons
local oldButton = itemFrame:FindFirstChild(item.Name)
if oldButton then
oldButton:Destroy()
end
local newButton = itemFrame.TemplateButton:Clone()
newButton.Name = item.Name
newButton.TowerName.Text = item.Name
newButton.Image = item.Config.Image.Texture
newButton.LayoutOrder = item.Config.Price.Value
newButton.Parent = itemFrame
newButton.Visible = true
newButton.Activated:Connect(function()
local coinLeaderstats = player:WaitForChild("leaderstats").Coins
if coinLeaderstats.Value >= item.Config.Price.Value then
--coinLeaderstats.Value -= item.Config.Price.Value
local remoteItem = item.Config.Price
ReplicatedStorage.Remotes.SaveData:FireServer(remoteItem)
if item then
ReplicatedStorage.Remotes.ShopItemGiver:FireServer(item)
else
warn("erteyertert")
end
PurchaseSound:Play()
else
-- error
warn("Not enough coins!")
ErrorSound:Play()
end
end)
end
end
local function toggleShop()
shopUI.Container.Visible = not shopUI.Container.Visible
if shopUI.Container.Visible then
updateItems()
end
end
local function setupShop()
local prompt = Instance.new("ProximityPrompt")
prompt.RequiresLineOfSight = true
prompt.ActionText = "Shop"
if workspace.Shop:WaitForChild("ShopPart") then
prompt.Parent = workspace.Shop:FindFirstChild("ShopPart")
end
prompt.Triggered:Connect(toggleShop)
exit.Activated:Connect(toggleShop)
end
setupShop()