How do you set a script's source to a pre-existing table

So I have this table and i want to set a scripts source but it’ll telling me i cant pass it through because its an array

local keyframe = {
	["Left Arm1"] = {
		["Position"] = nil,
		["Orientation"] = nil
	},
	["Right Arm1"] =  {
		["Position"] = nil,
		["Orientation"] = nil
	},
	["Left Leg1"] =  {
		["Position"] = nil,
		["Orientation"] = nil
	},
	["Right Leg1"] =  {
		["Position"] = nil,
		["Orientation"] = nil
	}
}

It must be a string so
ill use [[ ]] so i can create a multi-line string.

script.Source = [[local keyframe = {
	["Left Arm1"] = {
		["Position"] = nil,
		["Orientation"] = nil
	},
	["Right Arm1"] =  {
		["Position"] = nil,
		["Orientation"] = nil
	},
	["Left Leg1"] =  {
		["Position"] = nil,
		["Orientation"] = nil
	},
	["Right Leg1"] =  {
		["Position"] = nil,
		["Orientation"] = nil
	}
}]]

Alright but how would I change those nils to their own values?

They will be set to nil and not a string its just the format that roblox requires to change the source of a script.

Yeah I know they will be set to nil but is there anyway that I can like pass a variable inside of those strings?

You can change the values to whatever you want. I dont quite understand you, more explanation maybe?

So basically I have a keyframe table outside and I change it’s values before I set the source, how can I transfer those values?

If you want a direct reference to the exact same table (memory position and everything), you’ll need to create a function to consume it

script.Source = [[local function Consume(table)
  ...
end

return Consume]]

If you don’t mind the object being a different table memory wise, do what msix suggested.

Im confused, Consuming? Could you give a little bit more info please?

When you create a table, it is allocated a memory location. If you then create a copy of that table by data, it wont equal the other table since their memory locations are different.

This is different to passing a reference to the table which will result in both scripts having the same table

What you want, however, is to create a new table in a module script using Script.Source, and this can simply be done by writing your table as normal, but wrapping it in a [[ ]] block, for example:

script.Source = [[local cat = {
  Meow = 5
  Greeting = "meow"
}]]

Type the same table when setting it and thats it?

@msix29 @metatablecatmaid

Guys I’m sorry it’s reallly hard to explain but, okay so basically I am changing the original keyframe table like this

local keyframe = {
      ["Left Arm1"] = {
         ["Position"] = nil,
         ["Orientation"] = nil
      },
      ["Right Arm1"] =  {
         ["Position"] = nil,
         ["Orientation"] = nil
      },
      ["Left Leg1"] =  {
         ["Position"] = nil,
         ["Orientation"] = nil
      },
      ["Right Leg1"] =  {
         ["Position"] = nil,
         ["Orientation"] = nil
      }
   }
   for i,v in pairs(workspace.astraIboy.HumanoidRootPart:GetChildren()) do
      if v:IsA("Attachment") and keyframe[v.Name] then
         keyframe[v.Name].Position = v.Position
         keyframe[v.Name].Orientation = v.Position
      end
   end

The reason I cant set it manually is because i dont know what the value is gonna be ever

Do you guys understand now?

Ohhh alright i think you should use JSONEncode and Decode room change the table till a script so you make it as the source and change the source back as a table with JSONDecode

Wait so your saying json encode the table, then set the source to that and then get another script and json decode that script’s source?

No no from same script when u encode it do
Script.Source = JSONDecode(script.source)
Just after one another so it happens fast

Ohhh okay, I did it but it gave me this error?

image

local mod = Instance.new("ModuleScript")
   mod.Parent = workspace
   mod.Source = HttpService:JSONEncode(keyframe)
   mod.Source = HttpService:JSONDecode(mod.Source)

Oh right ENCODE returns a string hmmmmm

1 Like

Oh my gosh this is hopeless :frowning:

I gtg sleep now, i have an idea although it will take a bit of coding, replace the { with a special character and the } with another using string.gsub and when encoding do {encodedthing} and remove them at the end, complicated, when i wake up I’ll try to make it if you didnt understand me, which u obviously didnt with this explanation lol

1 Like

Thank you so much for all your time and you help, hopefully we can fix it soon

1 Like