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

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

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

✓ Accepted answer

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

1 comment

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

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

1 comment

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

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