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

how do I tell the difference between two parts with the same name?

Asked by 2 years ago

I have two parts in replicated storage and I want to keep their name the same so when I take one to workspace the same script will work on both parts, is there a way to just bring the first one or just bring the second into workspace.

0
I doubt this will do anything, but try putting something random in that object and rename it like apple, then write this game.ReplicatedStorage.Part.apple.Parent, stupid solution, I know, that’s why I commented. Sabailuridze 126 — 2y

2 answers

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

Try changing their properties. It could either be their color, size, position, material, texture, children, reflectance, transparency, orientation, etc. Another way is to put them in a folder or model, and use math.random. The third way is to make different attributes between each other like Part1 has the attribute "IsPart1" while Part2 has the attribute "IsPart2".

1st Way:

Player.Character.Touched:Connect(function(touchedPart)
    if touchPart.Name == "PartWithSameName" then
        if touchPart.BrickColor == "Really red" then
            print("Player touched Part1")
        elseif touchPart.BrickColor == "Really blue" then
            print("Player touched Part2")
        end
    end
end)

2nd Way:

local PartsWithSameName = game.ReplicatedStorage.PartsWithSameName

for i, v in pairs(PartsWithSameName:GetChildren()) do
    locan randomNumber = math.random(1, 2)
    if randomNumber == 1 then
        print("The game chose Part1")
        PartsWithSameName[randomNumber]:Clone().Parent = game.Workspace
    elseif randomNumber == 2 then
        print("The game chose Part2")
        PartsWithSameName[randomNumber]:Clone().Parent = game.Workspace
    end
end

3rd Way:

Player.Character.Touched:Connect(function(touchedPart)
    if touchPart.Name == "PartWithSameName" then
        if (touchPart:GetAttribute("IsPart1") ~= nil) then
            print("Player touched Part1")
        elseif (touchPart:GetAttribute("IsPart2") ~= nil) then
            print("Player touched Part2")
        end
    end
end)
Ad
Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

Try using Math.Random()

Heres an example:

local Part = script.Parent

Part.Touched:Connect(Function()
      local ExplodeOrNot = math.Random(1,2)

      if ExplodeOrNot == 1 then
         print("The part did not explode")
    end
 if ExplodeOrNot == 2 then
         print("The part Exploded!")
    end
end)

I also forgot to mention that you can also create two variables on one line like this: local Part1, Part2 = ----Stuff here

Answer this question