How would I get this local script to work with two parts?

I’m trying to make a local script that when someone touches a part, the part disappears on their screen, but I’m trying to make this command for multiple parts. For example, I have two parts, part 1 and part 2 and they both have the same name, I touch part one, and the part disappears, after touching part one I try to touch part two but when I touch part two doesn’t disappear.

This is the local script I’m using.

local part = game.Workspace.sunfragment – part
local Player = game.Players.LocalPlayer

part.Touched:Connect(function(PartTouched)
if PartTouched.Parent.Name == Player.Name then – If the players name = the player then it destorys the part on his client and not others
part:Destroy()
end
end)

It is located in starter player scripts. And I have not yet found any solutions to this problem.

2 Likes

So, you’re trying to make a part dissapear on your screen but appear on other’s screen?

I made it so it disappears on player 1 screen but not player 2 I’m trying to make it, so the code is not a onetime thing. As soon as player 1 touches the part one, he tries to touch part two, put touching part two does nothing, part two and one have the same name.

can you explain a lil more cuz i can’t wrap my head around

I’m sorry for the confusion ill explain more.

I don’t know what you mean but from all that i got this :man_shrugging:

for _,SunFragment in pairs(workspace:GetChildren()) do
     SunFragment.Touched:Connect(function(hit)
           if hit.Parent:FindFirstChild("Humanoid") and SunFragment.Name == "sunfragment" then
                 SunFragment:Destroy()
           end
     end)
end

oh so he meant like if u touch a part it gets destroyed…?

i really dont know

3131313131313123131

I tried to make it so if i touch a part it gets destroyed which the part gets destroyed, but when i touch another part with the same name it doesnt get destroyed?

so u want to scan every part that was touched with the same name and destroy it?

yeah so you probably meant this

2 Likes

Yea I was trying to get something like that but couldn’t figure out how to get it to work. I’m still learning how to code so this was all pretty confusing to me.

2 Likes

instead of looping through everything in workspace and checking the names, you could just make a folder of fragments and loop through that folder

local Players = game:GetService("Players")
local Fragments = workspace:WaitForChild("SunFragments"):GetChildren() 

-- Loop through the 'SunFragments' folder
for _, Fragment in ipairs(Fragments) do
  Fragment.Touched:Connect(function(hit)
    if Players:GetPlayerFromCharacter(hit.Parent) then
      Fragment:Destroy()
    end
  end)
end
3 Likes

much better

30003030303030333333333333333

1 Like

Thanks, I’m trying to make the code as simple as possible where it wont be a mess later on, this is gonna be a huge help!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.