working on some sort of inventory script, and basically
local player = game.Players.LocalPlayer
local inv = game.ReplicatedStorage.PlayerInvetorys:WaitForChild(player.Name .. "Folder")
local CurrentInv = inv:GetChildren()
local C = script.Parent.ItemFrame
print(CurrentInv)
for i, v in ipairs(CurrentInv) do
local K = C:Clone()
K.Text = v.Name
K.Visible = true
K.Parent = C.Parent
end
inv.ChildAdded:Connect(function(Child)
local K = C:Clone()
K.Text = Child.Name
K.Visible = true
K.Parent = C.Parent
end)
(all guns are stored in one folder)
It makes a new button in the gui, although when i click the gui i want it to move the exact gun from the ChildAdded / Table that the button was made from, and not just some random gun with the same name in the inventory
Since you’re looking to bind an action, you’ll need the event of the button being clicked.
And since the action is going to take place in two different functions with local variables (the ChildAdded function and the loop), we’ll go ahead and make an entirely separate function which they both can connect to.
Because the function is going to be outside of the other functions, we will have to supply it with the local parameters from the original function (v / Child).
function MoveItem(Item)
end
In each function we have to bind the event to call the MoveItem function with its supplying parameter.
K.MouseButton1Click:connect(function()
MoveItem(v) --Change v to Child for the second function.
end)
This is an example of what it could look like if you wanted an item to swap between two locations. If you want something like equipping, you will have to utilize the Humanoid EquipTool() and UnequipTools() functions instead.
local player = game.Players.LocalPlayer
local inv = game.ReplicatedStorage.PlayerInvetorys:WaitForChild(player.Name .. "Folder")
local CurrentInv = inv:GetChildren()
local C = script.Parent.ItemFrame
print(CurrentInv)
function MoveItem(Item)
if not Item then warn("Item does not exist!") return end
local IntendedParent1 = inv
local IntendedParent2 = nil --Change this to the location you want the item to move to.
if not IntendedParent1 or not IntendedParent2 then warn("PARENT LOCATION MISSING") return end
if Item.Parent == IntendedParent1 then
Item.Parent = IntendedParent2
elseif Item.Parent == IntendedParent2 then
Item.Parent = IntendedParent1
elseif Item.Parent then
warn("Item was found in an unapproved location! (" ..tostring(Item.Parent.Name)..")" )
Item.Parent = IntendedParent1 --In the case it gets lost, it will send it back to IntendedParent1 (inv). You can change this to wherever you want.
else
warn("Item was deleted!")
end
end
for i, v in ipairs(CurrentInv) do
local K = C:Clone()
K.Text = v.Name
K.Visible = true
K.Parent = C.Parent
K.MouseButton1Click:connect(function()
MoveItem(v)
end)
end
inv.ChildAdded:Connect(function(Child)
local K = C:Clone()
K.Text = Child.Name
K.Visible = true
K.Parent = C.Parent
K.MouseButton1Click:connect(function()
MoveItem(Child)
end)
end)
local player = game.Players.LocalPlayer
local folder = game.ReplicatedStorage.PlayerInvetorys[player.Name .. "Folder"]
local itemFrame = script.Parent.ItemFrame
local frames = {}
local function AddItem(instance)
local frame = itemFrame:Clone()
frame.Text = instance.Name
frame.Visible = true
frame.Button.Activated:Connect(function()
print("Clicked", instance.Name)
end)
frame.Parent = itemFrame.Parent
frames[instance] = frame
end
local function RemoveItem(instance)
frames[instance]:Destroy()
frames[instance] = nil
end
for i, child in ipairs(folder) do AddItem(child) end
folder.ChildAdded:Connect(AddItem)
folder.ChildRemoved:Connect(RemoveItem)
Alright i followed this and set it up as followed:
local player = game.Players.LocalPlayer
local inv = game.ReplicatedStorage.PlayerInvetorys:WaitForChild(player.Name .. "Folder")
local CurrentInv = inv:GetChildren()
local C = script.Parent.ItemFrame
print(CurrentInv)
function equipitem(item)
if not item then warn("Wheres My Gun At?") return end
game.ReplicatedStorage.TotallyCoolEvents.EquipGun:FireServer(item)
end
for i, v in ipairs(CurrentInv) do
local K = C:Clone()
K.Text = v.Name
K.Visible = true
K.Parent = C.Parent
K.MouseButton1Click:Connect(function()
equipitem(v)
end)
end
inv.ChildAdded:Connect(function(Child)
local K = C:Clone()
K.Text = Child.Name
K.Visible = true
K.Parent = C.Parent
K.MouseButton1Click:Connect(function()
equipitem(Child)
end)
end)
Server Script
game.ReplicatedStorage.TotallyCoolEvents.EquipGun.OnServerEvent:Connect(function(plr, d)
local Char = plr.Character
if not d then return end
if Char:FindFirstChildWhichIsA("Tool") then
d.Parent = plr.Backpack
else
d.Parent = Char
end
end)
Just wondering if theres any way this could break or something
I would probably do this as a server script instead because I think you still need to equip it on the humanoid in order for it to be used by the player.
game.ReplicatedStorage.TotallyCoolEvents.EquipGun.OnServerEvent:Connect(function(plr, d)
local Char = plr.Character
Human = Char:FindFirstChild("Humanoid")
if not d or not Human then return end
if not d:IsA("Tool") then return end
if d.Parent == Char then
Human:UnequipTools()
d.Parent = plr.Backpack
else
Human:UnequipTools()
d.Parent = Char
Human:EquipTool(d)
end
end)