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

Can you have multiple of an object with the same name?

This helps you understand:

i want to make a bunch of Parts but with the same name i want the script to pick every object named Part1 i don't wana rename the Parts.

This is the problem: the code i made is very easy and short but the point is it does not pick up the objects Named Part1.

(Keep in mind this i can not just make local part1,part2... , that is just going to take a long time to do. there are 50+ parts)

Code:

local Part1 = game.Workspace.All.Part1
local text_Button = game.StarterGui.ScreenGui.TextButton

function onTouched(Part1)
	local h = Part1.Parent:findFirstChild("Humanoid")
	if h~=nil then
		print("Touched")
		text_Button.Visible = true
		
	end
end

Part1.Touched:Connect(onTouched)

1 comment

0

Just a fix for ur script: change if h~=nil then to if h then

1 answer

✓ Accepted answer

Hello, you may use for loops in pairs for that. Here's the script:

local All = game.Workspace.All
local text_Button

function onTouched(Part1)
    local h = Part1.Parent:FindFirstChild("Humanoid")
    if h ~= nil then
        print("Touched")
        text_Button.Visible = true
    end
end

game.Players.PlayerAdded:Connect(function(player)
    text_button = player.PlayerGui.ScreenGui:WaitForChild("TextButton")
    for i, Part1 in pairs(All:GetChildren()) do
        if Part1.Name == "Part1" then
            Part1.Touched:Connect(onTouched)
        end
    end
end)

Also notice that, you can't use StarterGui when you're in a different script, please use player.PlayerGui. And never use game.Players.LocalPlayer on a server script, use game.Players.PlayerAdded:Connect(function(player)

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