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

How do i modify a Instance (That was made of script) with a script?

How do i modify a Instance (That was made of script) with a script? Like change the name, size, Position, Color, material

First Code:

local clickdetector = script.Parent:WaitForChild("ClickDetector")

clickdetector.MouseClick:Connect(function()
part = Instance.new("Part",game.Workspace)
local posz = math.random(-62,62)
local posx = math.random(-62,62)
part.Position = Vector3.new(posx,0,posz)
part.Size = Vector3.new(100,100,100)
part.Name = "COLORCHANGE"	
end)

Second Code (Other Script):

local clickdetector = script.Parent:WaitForChild("ClickDetector")

clickdetector.MouseClick:Connect(function()
local part = Workspace:FindFirstChild("COLORCHANGE")
if part then
part.BrickColor = BrickColor.Random()
end)

1 answer

✓ Accepted answer

If the part you are wanting to modify is the one that the first script is creating, then you can change the values that are being set there.

In the first script, it creates a part and sets its Position, Size, and Name. Make sure to set the variable part as a local variable. Lets modify more properties.

local clickdetector = script.Parent:WaitForChild("ClickDetector")

clickdetector.MouseClick:Connect(function()
	--create part
	local part = Instance.new("Part")
	local posz = math.random(-62,62)
	local posx = math.random(-62,62)
	--change Position property
	part.Position = Vector3.new(posx,0,posz)
	--change Size property
	part.Size = Vector3.new(100,100,100)
	--change Size property
	part.Name = "COLORCHANGE"   
	
	--Lets change its Color and material too

	--change Color property
	part.BrickColor = BrickColor.random()
	--change Material property
	part.Material = "Neon"

	part.Parent=game.Workspace
end)

In your second script, the part will change its color to a random one. Here it is changing the part's color property Lets make it change its material to Plastic

There is also an error in your second script. You forgot to add an end to finish the if part then statement.

local clickdetector = script.Parent:WaitForChild("ClickDetector")

clickdetector.MouseClick:Connect(function()
	local part = Workspace:FindFirstChild("COLORCHANGE")
	if part then
		part.BrickColor = BrickColor.Random()
		--change material
		part.Material="Plastic"
	end
end)

A good practice is to not use the second parameter of Instance.new() More in depth reason

Sorry if your main question was to figure out why the script wasn't working. I thought you were asking how to change properties.

1 comment

0

Okay, Thank You!

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