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

Is it possible to make a new instance script and put a code in it? (SOLVED)

Ok so I want to make a script where a player picks up a stick it makes a imagelabel and value into the player inventory, however, it works perfectly except the part I want to make a line of code where I can make a new Instance localscript into the textlabel of the image to put a while loop to keep updating the value of the Sticks. Is this possible? Help will be very much appreciated

local cd = script.Parent.ClickDetector
local id = 'rbxassetid://04632925787'
local image = 'http://www.roblox.com/asset/?id=1091279830'
local db = false
cd.MouseClick:connect(function(op)
	if db == false then
		local player = game:GetService('Players'):FindFirstChild(op.Name)
		if player ~= nil then
			db = true
			print(player)
			local gui = player:WaitForChild('PlayerGui')
			local animation = Instance.new('Animation',script.Parent)
			animation.AnimationId = id
			local hum = player.Character:WaitForChild('Humanoid')
			hum:LoadAnimation(animation):Play()
			hum.WalkSpeed = 0
			hum.JumpPower = 0
			local inv = gui:FindFirstChild('Inventory'):WaitForChild('MainFrame')
			if inv:FindFirstChild('Sticks') == nil then
				local img = Instance.new('ImageLabel',inv)
				img.Size = UDim2.new(0, 100,0, 100)
				img.Image = image
				img.Name = 'Sticks'
				img.BackgroundTransparency = 1
				local val = Instance.new('IntValue',img)
				val.Name = 'Amount'
				val.Value = val.Value + 1
				local info = Instance.new('TextLabel',img)
				info.Txt.Disabled = false
				info.Name = 'Title'
				info.Text = 'Sticks: '.. val.Value
				info.TextScaled = true
				info.TextColor3 = Color3.fromRGB(255,255,255)
				info.Size = UDim2.new(0,100,0,25)
				info.Position = UDim2.new(0,0,1,0)
				info.BackgroundTransparency = 1
				info.Font = Enum.Font.SourceSansSemibold
				local code = Instance.new('LocalScript',info)
				--code.I dont know what to put here = ''
			else
				local amnt = inv:WaitForChild('Amount')
				amnt.Value = amnt.Value + 1
			end
			wait(1)
			hum.WalkSpeed = 16
			hum.JumpPower = 50
			db = false
			script.Parent:Destroy()
		end
	end
end)

I found a solution and surprisingly it was actually easy! For those who want to know, you could just use loadstring()()
Example:

--ServerScript in workspace

local sc = Instance.new('Script',workspace)
local code = Instance.new('StringValue',sc)

code.Value = [[
	while true do wait()
		print('Hi world')
	end
]]
loadstring(code.Value)() --Add a double paranthesis, Idk why but it needs it

Output: Hi world[How many times it said it]

3 comments

0

It's impossible (From what I know) to create a script AND put a code inside of it.

0

You can always make the Text Label and clone it by also enabling the local script

0

Its actually possible, I just edited it

1 answer

Have the script ready-made but disabled, then Enable it when needed. That's the simplest and easiest way I can think of...

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