Edit & Save Script Source Code

Hello developers!

I currently have a system that allows a script to interact with a module script using either the addItem of updateItem function. When one of those is used, an item is added to the “items” table in the module script. Although the item is added to the table when playtesting, it does not save to the actual script source code. When an item is added to the table, I would like that new table to essentially overwrite the old one, to have the table be updated and to have that update the code in studio.

I know there are methods such as script.source that can be used in the command line, but that doesn’t seem to work for my use case.

I’m not sure how easy that is to understand. Please ask questions if it’s unclear what I want.

Script

local t = {}
local items = {
	{
		Id = 1,
		Name = "",
		Type = "Misc.",
		Price = 0,
		Image = 123,
	},
	{
		Id = 2,
		Name = "",
		Type = "Misc.",
		Price = 0,
		Image = 123,
	},
}

t.updateItem = function(nId, nName, nType, nPrice, nImage)
	for _, item in pairs(items) do
		if item.Id == nId then
			print(item.Id)

			item.Name = nName
			item.Type = nType
			item.Price = nPrice
			item.Image = nImage
			
			if not item.Name then
				item.Name = "nil"
			end
				
			if not item.Type then
				item.Type = "nil"
			end
				
			if not item.Price then
				item.Price = 0
			end
			
			if not item.Image then
				item.Image = 0
			end
		end
	end
	print(items)
	--return items
end

t.addItem = function(nId, nName, nType, nPrice, nImage)
	table.insert(items,nId, {nId,nName,nType,nPrice,nImage})

	print(items)
	--return items
end

return t
1 Like

Can you try to re-explain Because i can solve it

Instead of just printing the new table with the added items, I would like it to actually show up in the module script, in the source code.

Add this:

t.GetItems = function()
   return items
end

Remove this part of your script.

Fixed one:

t.updateItem = function(nId, nName, nType, nPrice, nImage)
	for _, item in pairs(items) do
		if item.Id == nId then
			item.Name = nName
			item.Type = nType
			item.Price = nPrice
			item.Image = nImage
		end
	end
end

t.addItem = function(nId, nName, nType, nPrice, nImage)
	table.insert(items,{nId,nName,nType,nPrice,nImage})
end

You can’t, the things you put in the table is the preset items. Any items added afterward will be stored in memory and environment, not the script.

1 Like

I appreciate the optimization, but none of that is actually updating the code of the script.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.