Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Attempt to code a script inside another script, not working..??

Hello, I am making an inventory system and I am trying to make a script in a script so this is what I am trying to do:

-- lets start...
-- the frame holds 25 imagebuttons
function MakeFrames()
	local HowManyFrames = 10
	local b = 0
	repeat
		local addValue = UDim2.new(b,0,0,0)
		local il = Instance.new("ImageButton", script.Parent)
		il.Size = UDim2.new(0.04, 0,0.845, 0)
		il.Position = UDim2.new(0,0,0,0) + addValue
		b = b + 0.04
		HowManyFrames = HowManyFrames - 1
                local NEWSCRIPT = Instance.new("Script", il)
                NEWSCRIPT.script = "print(b.Value)"
	until HowManyFrames == 0
-- replicated
end

MakeFrames()

The error is on line 13 and 14 , script is not a valid member of script, what can I do?

4 comments

0

Correct me if I am wrong but you named the new script "NEWSCRIPT", and not script? So you'd call it using 'il.NEWSCRIPT' and not "il.script"

0

Still, I don't exactly understand why you've made your system like this? Care to explain?

0

You cannot set a source of a script, without command bar permissions or plugin permissions.

0

So wait, I fixed what T0XN said... The script is to create imagebuttons in a frame and insert a whole new script with a code i want in it, I will have to code the code but I am trying to figure out a way to do that...

1 answer

✓ Accepted answer

The error you are encountering is because there is no script property of scripts. There is a Source, but you cannot assign to that (or access it what-so-ever)..

No matter what you wish to accomplish, you shouldn't need to create scripts within scripts (which requires the use of 'loadstring'; it's server-side only and so not good for GUIs). You can :Clone() a pre-existing script (and parent it where-ever you like) or you can manage a table of ImageButtons (or whatever you want) all from one script (I thoroughly recommend doing this). ex:

for i = 1, 25 do
	local imageButton = Instance.new("ImageButton")
	imageButton.Size = UDim2.new(0.04, 0, 0.845, 0)
	imageButton.Position = UDim2.new(0.04 * (i - 1), 0, 0, 0)
	imageButton.Parent = script.Parent
	imageButton.MouseButton1Down:Connect(function(x, y)
		print("Image button #" .. i .. " clicked at", x, y)
	end)
end

The nice thing with putting it all in one script is that the code can use functions defined in the current script.

3 comments

0

You can, but not the way he wants to.

0

so i have to create a totally different script and clone it then put it in the text button?

0

You can, but I still recommend keeping it all in one script. I've shown a simple example of how to do that in the post (which I just updated).

This discussion is preserved as it was. New answers, comments, and votes are closed.