So I am trying to create inventory and whenever item gets added into the inventory folder it should appear on inventory GUI but childAdded is not firing. can anyone tell me why?
local Players = game:GetService("Players")
local InventoryEvent = game.ReplicatedStorage.InventoryEvent
local function playerSetup(player)
local Inventory = Instance.new("Folder", player);
Inventory.Name = "Inventory";
Inventory.ChildAdded:Connect(function(item)
print("sending...");
InventoryEvent:FireClient(player, item.name, true);
end)
InventoryEvent.OnServerEvent:Connect(function(player, ItemName, Value)
if(Value == false) then
local SelectedItem = player.Inventory:FindFirstChild(ItemName);
SelectedItem.Parent = game.Workspace
SelectedItem.Model.PrimaryPart = SelectedItem.Model.Primary;
SelectedItem.Model:SetPrimaryPartCFrame(player.Character.HumanoidRootPart.CFrame +Vector3.new(0, 0, 2));
else
end
end)
-------------------------------------------
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Value = 5000
coins.Parent = leaderstats
local Level = Instance.new("IntValue")
Level.Name = "Level"
Level.Value = 1
Level.Parent = leaderstats
local Wins = Instance.new("IntValue")
Wins.Name = "Wins"
Wins.Value = 0
Wins.Parent = leaderstats
local Kills = Instance.new("IntValue")
Kills.Name = "Kills"
Kills.Value = 0
Kills.Parent = leaderstats
end
Players.PlayerAdded:Connect(playerSetup)
Itâs not firing because the Parent is workspace and not the designated Inventory folder. If you need the objects visible, then move the folder to workspace.
purchasebutton.MouseButton1Click:Connect(function()
if player.leaderstats.Coins.Value >= swords[position]:GetAttribute("Price") then
player.leaderstats.Coins.Value -= swords[position]:GetAttribute("Price");
local clone = swords[position]:Clone();
clone.Parent = player.Inventory;
end
end)
Nope It Doesnt work, but i tried to add sword via remoteEvent and it gave me this error.
Attempt to set parent of Players.GMAL222 to Players.GMAL222.Inventory would result in circular reference
this is code
purchasebutton.MouseButton1Click:Connect(function()
if player.leaderstats.Coins.Value >= swords[position]:GetAttribute("Price") then
player.leaderstats.Coins.Value -= swords[position]:GetAttribute("Price");
local clone = swords[position]:Clone();
print(clone);
game.ReplicatedStorage.AddItem:FireServer(player, clone);
end
end)
local AddItem = game.ReplicatedStorage.AddItem
AddItem.OnServerEvent:Connect(function(player, sword)
sword.Parent = player.Inventory
end)
OnServerEvent instantly takes player as the first argument, remove the player object in FireServer. You have to create the sword on the server, the sword would be nil as objects created by the client doesnât replicate to the server
Change this to item.Name
Again, OnClientEvent does not take player as its first argument, so remove the player from the function (NOT FireClient)
It should work now