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

copy part with script and paste it... the copied one doesnt work... why? help :(

Asked by 6 years ago
Edited 6 years ago

i made a part that when u touch it...it change color to red the script works pefectly......i want to copy the part and paste it so many times....but when i do it the copied once are not working...only the origial part is working....this is my script

brick = game.Workspace.Part
function onTouch()
    brick.BrickColor = BrickColor.new("Really red") --You have to spell it 
end
brick.Touched:connect(onTouch)

please tell me if there is something i should do to make the others part works when we touch them :( Thank you so much :D

2 answers

Log in to vote
1
Answered by
mattscy 3725 Moderation Voter Community Moderator
6 years ago

The script only ever works for the one part because it finds the first part in workspace to use. To fix this, instead of going

brick = game.Workspace.Part

you can go

brick = script.Parent

which will mean the part that the script is inside of.

It may also be more efficient to have one script in the game, and put all the parts inside a model or folder called TouchParts, and do something like this:

for _,v in pairs(workspace.TouchParts:GetChildren()) do
    v.Touched:Connect(function()
        v.BrickColor = BrickColor.new("Really red")
    end)
end
0
OMG you are genius Inever thought about having one script LOL I feel dumb....thank you so much for helping you answered two of my questions :D...thank you Romy_99 6 — 6y
Ad
Log in to vote
1
Answered by
Vulkarin 581 Moderation Voter
6 years ago

Doesn't work because you are only referencing one part being game.Workspace.Part, if you actually wanted to clone them then you'd do

script.Parent.Touched:Connect(function()
    script.Parent.BrickColor = BrickColor.new("Really Red")
end)

But it seems sorta silly to have a ton of scripts doing the same thing, if you put all the parts into one parent (could be a folder, a model) you could do

for _, part in pairs(workspace.Parts:GetChildren()) -- In this example all parts must be in a child of workspace named "Parts"
    part.Touched:Connect(function()
        part.BrickColor = BrickColor.new("Really Red")
    end)
end
0
Yes you are right....I didnt know that at the begining because I'm new in scripting and I make so many mistake....I will make sure not to make silly things in Future LOL....thank you so much for helping :D Romy_99 6 — 6y

Answer this question