This is a storage script that opens up a GUI whenever a player touches the said container/object. What I'm trying to do is add a preset item for players to take.

local player = script.Parent.Parent.Parent;
local chest = script.Parent.Chest.Value;
local gui = script.Parent;
local window = script.Parent.Window;
local options = window.Options;
local Tools = script.Parent.Tools.Value;
local selected = {};

options.Close.MouseButton1Down:connect(function()
	script.Parent.Parent = chest;
end);

options.Take.MouseButton1Down:connect(function()
	if(selected[1]~=nil)then
		selected[1].Parent = player.Backpack;
		selected[1] = nil;
		displayBackpackItems();
		displayChestItems();
		script.Collect.Pitch = 0.95;
		script.Collect:Play();
	end
end);

options.Deposit.MouseButton1Down:connect(function()
	if(selected[2]~=nil)then
		selected[2].Parent = Tools;
		selected[2]=nil;
		displayBackpackItems();
		displayChestItems();
		script.Collect.Pitch = 0.85;
		script.Collect:Play();
	end
end);

function removeItems(fr)
	for i,v in pairs(fr:GetChildren())do
		v:remove();
	end
end

function displayChestItems() --- I believe this is where our focus should be, as this displays the items CURRENTLY within the chest. I'm trying to put a preset item in here.
	removeItems(window.Items.Chest);
	local sizeY;
	sizeY = 1/#Tools:GetChildren();
	for i,v in pairs(Tools:GetChildren())do
		local btn = Instance.new("TextButton");
		btn.Name = v.Name;
		btn.Text = v.Name;
		btn.TextColor3 = Color3.new(01,01,0);
		btn.BackgroundTransparency = 0.5;
		btn.BackgroundColor3 = Color3.new(0,0,0);
		btn.Size = UDim2.new(1,0,sizeY,0);
		btn.Position = UDim2.new(0,0,sizeY*(i-1),0);
		btn.Parent = window.Items.Chest;
		btn.MouseButton1Down:connect(function()
			selected[1] = v;
		end);
	end
	wait();
end

function displayBackpackItems()
	removeItems(window.Items.Backpack);
	local sizeY;
	sizeY = 1/#player.Backpack:GetChildren();
	for i,v in pairs(player.Backpack:GetChildren())do
		local btn = Instance.new("TextButton");
		btn.Name = v.Name;
		btn.Text = v.Name;
		btn.TextColor3 = Color3.new(01,01,0);
		btn.BackgroundTransparency = 0.5;
		btn.BackgroundColor3 = Color3.new(0,0,0);
		btn.Size = UDim2.new(1,0,sizeY,0);
		btn.Position = UDim2.new(0,0,sizeY*(i-1),0);
		btn.Parent = window.Items.Backpack;
		btn.MouseButton1Down:connect(function()
			selected[2] = v;
		end);
	end
	wait();
end
displayBackpackItems();
displayChestItems();
local torso;
while(script.Parent.Parent.Name=="PlayerGui")do
	wait();
	torso = game.Players.LocalPlayer.Character:FindFirstChild("Torso");
	if(torso)then
		if((chest:GetChildren()[1].Position-torso.Position).magnitude >= 6)then
			gui.Parent = chest;
			script.Parent.Window.Visible = true
			break;
		end
	else
		gui.Parent = chest;
		break;
	end
end

1 comment

0

You can't just slap a code and expect us to fix it. Tell us about where the error is and don't give us the whole script if it has nothing to do with the error.