Help with tables

  1. What do I want to achieve? when a player buys an item, all the details of the item 'nd the next item will be there. But the items there after should’nt have the purchase details and the image of the item will be blacked out
  2. What is the issue? it shows this error
Players.itzmerose_12.PlayerGui.tools_gui.tools_frame.ScrollingFrame.1.tommy:4: invalid argument #1 to 'pairs' (table expected, got nil)
  1. What solutions have you tried so far? i created a table and it didnt work
--part 1 of script
local module = {}
for i,tool in pairs(game.StarterPack:GetChildren()) do
	
	dirtovalk = game.StarterPack["Valkyrie of dirt"]
	local dirt = {"Valkyrie of Dirt" , 0 , dirtovalk.TextureId}
	
end

return module
--part 2
local rep = game:GetService("ReplicatedStorage")
local detail = require(rep:FindFirstChild("details"))

for index, value in pairs(detail.dirt) do
	script.Parent.MouseButton1Click:Connect(function()
	 script.Parent.Parent.Parent.price.Text = tostring(detail.dirt(1))
		end)
	
end

the error sort of explains it, whatever you’re passing into pairs is nil.

im guessing you’re trying to reference the table “dirt” from the first module script. the reason it is nil is because it needs to be a member of the modules table, and right now you are storing it as just a local variable inside the module script.

try changing it from “local dirt = …” to “module.dirt = …”

tried but doesn’t work:
Players.itzmerose_12.PlayerGui.tools_gui.tools_frame.ScrollingFrame.1.tommy:6: attempt to index nil with ‘dirt’

script:

local module = {}
for i,tool in pairs(game.StarterPack:GetChildren()) do
	
	dirtovalk = game.StarterPack["Valkyrie of dirt"]
	module.dirt = {"Valkyrie of Dirt" , 0 , dirtovalk.TextureId}
	
end

return module
local rep = game:GetService("ReplicatedStorage")
local detail = require(rep:FindFirstChild("details"))

for index, value in pairs(detail.dirt) do
	script.Parent.MouseButton1Click:Connect(function()
	 script.Parent.Parent.Parent.price.Text = tostring(detail.module.dirt[1])
		end)
	
end

can you suggest other methods? thank you

What exactly are you trying to do here?

In the tostring(detail.module.dirt[1]) part, you do not need to reference the module. The detail variable is already the module table you set in the ModuleScript. Therefore, your script should be:

local rep = game:GetService("ReplicatedStorage")
local detail = require(rep:FindFirstChild("details"))

for index, value in pairs(detail.dirt) do
	script.Parent.MouseButton1Click:Connect(function()
	 script.Parent.Parent.Parent.price.Text = tostring(detail.dirt[1])
		end)
	
end

The code can be improved, for example the for loop should be inside the MouseButton1Click event. Therefore the improved code is:

local rep = game:GetService("ReplicatedStorage")
local detail = require(rep:FindFirstChild("details"))

script.Parent.MouseButton1Click:Connect(function()
  for index, value in pairs(detail.dirt) do
	 script.Parent.Parent.Parent.price.Text = tostring(detail.dirt[1])
  end
end)
1 Like


thanks now my ui is working but i cant make the image blacked out. To be clear look at the above image


im trying to make this effect (look at the gui)

help me with my script please
thank you
my code:

game.ReplicatedStorage.Remotes.PlayerLoaded:FireServer()
print("PlayerLoaded")
local player = game.Players.LocalPlayer
local number = 1
local strings = tostring(number)
game.ReplicatedStorage.Remotes.BeginArrangement.OnClientEvent:Connect(function()
	print("Got")
	repeat
		local image = game:GetService("ReplicatedStorage").ToolsImage:WaitForChild(strings)

		if image:IsA("ImageLabel") then
			print(image.Name)
			local clone = game.ReplicatedStorage.Template:Clone()
			clone.ToolImageDisp.ImageLabel.Image = image.Image
			clone.Name = image.Name
			clone.CostDisplay.Value = image.Cost.Value
			clone.ToolName.Value = image.ToolName.Value
			clone.Parent = script.Parent
			clone.ToolImageDisp.ImageLabel.ImageColor3 = Color3.fromRGB(0,0,0) 

			if clone.Name == "1" then
				local sidemenu = script.Parent.Parent
				sidemenu.tool_img.Image = image.Image
				sidemenu.tool_img.ImageColor3 = Color3.fromRGB(255,255,255)
				sidemenu.status.Text = "Equip"
				sidemenu.price.cost.Value = 0
				sidemenu.price.Text = "Cost : 0"
				sidemenu.name.Text = clone.ToolName.Value

			end

			if player.OwnedItems:FindFirstChild(clone.ToolName.Value) then
				clone.ToolImageDisp.ImageLabel.ImageColor3 = Color3.fromRGB(255,255,255)
			end

			local equipped = player:WaitForChild("Equipped")
			if equipped.Value~= nil then
				if clone.ToolName.Value == equipped.Value then
					script.Parent.tool_img.Image = image.Image
					script.Parent.tool_img.ImageColor3 = Color3.fromRGB(255,255,255)
					script.Parent.ststus.TextButton.Text = "Equipped"
					script.Parent.price.Cost.Value = clone.CostDisplay.Value
					script.Parent.price.Text = "Cost : "..script.Parent.price.cost.Value
					script.Parent.name.Text = clone.ToolName.Value
					game.ReplicatedStorage.Remotes.EquipTool:FireServer(equipped.Value)
				end
			end

			local number = tonumber(clone.Name)
			if number ~= 1 then
				local previousnum = number-1
				local itemfound = script.Parent:FindFirstChild(previousnum)
				if itemfound then
					if player.OwnedItems:FindFirstChild(itemfound.ToolName.Value) then
						clone.ToolImageDisp.ImageLabel.ImageColor3 = Color3.fromRGB(255,255,255) 
					end 
				end
			end
		end
		number = number+1
	until number == game.ReplicatedStorage.TotalTools.Value + 1
end)
local player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()

	local templateno = tonumber(script.Parent.Parent.Name)
	local templatename = script.Parent.Parent.ToolName.Value
	local previousno
	local previousname
	if templateno ~=1 then
		previousno = templateno -1
		previousname = script.Parent.Parent.Parent:FindFirstChild(previousno).ToolName.Value
	else
		previousname = script.Parent.Parent.ToolName.Value
	end
	
	if player.OwnedItems:FindFirstChild(previousname) then
		
		local sidemenu = script.Parent.Parent.Parent.Parent.Parent.SideMenu
		sidemenu.ImageOftool.Image = script.Parent.Parent.ToolImageDisp.ImageLabel.Image
		sidemenu.ImageOftool.ImageColor3 = Color3.fromRGB(255,255,255)
		sidemenu.NameOftool.TextLabel.Text = script.Parent.Parent.ToolName.Value
		sidemenu.Cost.CostOfTool.Value = script.Parent.Parent.CostDisplay.Value
		sidemenu.Cost.CostDisplayer.Text = "Cost : "..sidemenu.Cost.CostOfTool.Value
		if player.Equipped.Value == script.Parent.Parent.ToolName.Value then
			sidemenu.BuyButton.TextButton.Text = "Equipped"
		elseif player.OwnedItems:FindFirstChild(templatename) then
			sidemenu.BuyButton.TextButton.Text = "Equip"
		else
			sidemenu.BuyButton.TextButton.Text = "Buy"
		end
	else
		local sidemenu = script.Parent.Parent.Parent.Parent.Parent.SideMenu
		sidemenu.ImageOftool.Image = script.Parent.Parent.ToolImageDisp.ImageLabel.Image
		sidemenu.ImageOftool.ImageColor3 = Color3.fromRGB(0,0,0)
		sidemenu.NameOftool.TextLabel.Text = "???"
		sidemenu.Cost.CostOfTool.Value = 0
		sidemenu.Cost.CostDisplayer.Text = "Cost : ???"
		sidemenu.BuyButton.TextButton.Text = "Can't Buy Now"
		
	end
end)
local module = {}
for i,tool in pairs(game.StarterPack:GetChildren()) do
	
	dirtovalk = game.StarterPack["Valkyrie of dirt"]
	module.dirt = {"Valkyrie of Dirt" , 0 , dirtovalk.TextureId}
	
end
for i,tool in pairs(game.ServerStorage:GetChildren()) do
	valk = game.ServerStorage.Tools.Valkyrie
	module.valkyrie = {"Valkyrie", 50, valk.TextureId}
end

return module

local rep = game:GetService("ReplicatedStorage")
local detail = require(rep:FindFirstChild("details"))

script.Parent.MouseButton1Click:Connect(function()
		for index, value in pairs(detail.dirt) do
		script.Parent.Parent.Parent.name.Text = tostring(detail.dirt[1])
		script.Parent.Parent.Parent.price.Text = tostring(detail.dirt[2])
		script.Parent.Parent.Parent.tool_img.Image = detail.dirt[3]
		end
	
end)