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

When two parts meet, how can I make only one of them execute this code?

I'm having trouble with this and I really don't know how to get around it. Both of the parts that touch each other are identical and they're both cloned objects, therefore they both hold this code. Is there a way I could only make it so one of those parts execute it?

wait(2)

function ontouch(part)
	
	if part.Name == "BombBit" then
		
		script.Disabled = true
		
		local bitCln = game.ServerStorage.BombBitTwo:Clone()
		bitCln.Parent = workspace.Bitz
		bitCln.Position = script.Parent.Position
		
		script.Parent:Destroy()
		
		
	end
	
end

script.Parent.Touched:Connect(ontouch)

2 answers

Going by what you said stating they are "cloned objects" and assuming they are cloned using obj:Clone() then you could simply change the name of one object using

local obj = obj:Clone
obj.Name = "different name"

In the case they are simply two cloned parts using the studios building features, you could simply:

  1. Right click the object in the explorer (hierarchy)

  2. Click "Rename"

  3. Change the name to something else

In the case that both above answers are wrong try adding an else statement or just ignore this answer.

THINK before you downvote my posts, do i just need to be told that what i said was incorrect or do i need a downvote?

4 comments

0

Both parts need the same name for the script to execute, the point is that when these two of the same objects collide, both get destroyed and a new part gets placed in.

0

Sorry I wasn't more clear with my question.

0

Wouldn't it not hurt to change a few lines of code? You could use or for example if name == "hello" or "hi" then - - if name is "hello" then it will execute but if it isn't AND it is hi then it will execute, other than that i can't help.

0

It's all good, I'll experiment a little bit more but thank you

I've done a similar thing in one of my games. The way I did it is to have a boolvalue in both objects, and have the script check if both objects boolvalues are false, then make them true.

Something like this:

script.Parent.Touched:Connect(function(hit)
	if hit.Name == "BombBit" and script.Parent.BoolValue.Value == false and hit.BoolValue.Value == false then
		script.Parent.BoolValue.Value = true
		hit.BoolValue.Value = true
		--- The rest of the code
	end
end)

9 comments

0

I'm unfamiliar with boolvalues so I'll look in to them. Thanks for the answer, I'll try it out.

0

I got this errpr, "BoolValue is not a valid member of Part", I think I may have messed something up.

0

wait i'm silly, forgot to add the actual boolvalue.

0

It works almost perfectly, the problem I have is only one of the pieces gets deleted, basically what happens is that two of these things collide and they both disappear while a new singular part appears. Is that possible to be scripted?

0

So what you mean is that since these 2 scripts are the same, it is creating 2 new parts when the originals collide, but you want only one part to be made instead?

0

One of the cloned parts gets deleted while the other does, only one of the new parts is supposed to appear and that already happens. I wish i had some form of image cause this sounds confusing to explain.

0

sorry that second does is supposed to be doesn't

0

From what I understand you want to do something like this? https://imgur.com/l51lrfQ

0

Okay its been 2 months but yeah that's it.

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