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.
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.
I don’t know what you mean but from all that i got this
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
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?
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.
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