Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago
Edited 5 years ago

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

Log in to vote
0
Answered by
DanzLua 2879 Moderation Voter Community Moderator
5 years ago

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.

0
Okay, Thank You! GodlyEricGamer1800 -100 — 5y
Ad

Answer this question