Can my function affect everything named "Example Name" in the Workspace?

  1. I am trying to make it so that every object in the workspace named “ShirtObject” gets affected by my script

  2. My issue is that it only works with one “ShirtObject” which was already present in the workplace before pressing play.

  3. I’m not sure why it doesn’t count the other shirts, it might be because they are cloned? Or does the script only work for one single object at a time?

This code is for when triggering a proximity prompt it adds it to your inventory.

local Pickup = workspace:WaitForChild("ShirtObject")

Pickup.ProximityPrompt.Triggered:Connect(function(player)
	local ShirtTool = game:GetService("ServerStorage").Shirt:Clone()
	
	if not player.Character:FindFirstChild("Shirt") and not player.Backpack:FindFirstChild("Shirt") then
		ShirtTool.Parent = player.Character
	end
end)

And this code below is for generating an infinite amount of “ShirtObjects” on an conveyor.

local ServerStorage = game:GetService("ServerStorage")
local Workspace = game:GetService("Workspace")

while true do
	-- Ensure the ShirtGenerator part exists in the Workspace
	local shirtGenerator = Workspace:FindFirstChild("ShirtGenerator")
	if not shirtGenerator then
		warn("ShirtGenerator part not found in Workspace.")
		return
	end

	-- Ensure the part to clone exists in ServerStorage
	local partToClone = ServerStorage:FindFirstChild("ShirtObject")
	if not partToClone then
		warn("PartToClone not found in ServerStorage.")
		return
	end

	-- Clone the part and set its position to the ShirtGenerator's position
	local clonedPart = partToClone:Clone()
	clonedPart.CFrame = shirtGenerator.CFrame
	clonedPart.Parent = Workspace

	-- Wait for a short duration before repeating
	wait(1)
end

Thank you!

1 Like

I can’t tell if your ProximityPrompt code is a script that is inside every single shirt object spawned, if so, that’s a bad coding practice especially with the use of ProximityPromptService. This code should work better for you.

This only other issue I could possibly see is if not player.Character:FindFirstChild("Shirt") and not player.Backpack:FindFirstChild("Shirt") then. As long as you only want the player to have one Shirt in their inventory, this is fine.

local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Workspace = game:GetService("Workspace")
local ProximityPromptService = game:GetService("ProximityPromptService")

-- Will Fire for ANY Proximity Prompt Triggered Including Spawned Ones
ProximityPromptService.PromptTriggered:Connect(function(TriggeredPrompt, User)
	-- Making Sure the Prompt's Parent is the ShirtObject and in the Workspace
	if TriggeredPrompt.Parent.Name == "ShirtObject" and TriggeredPrompt.Parent.Parent == Workspace then
		local ShirtTool = ServerStorage.Shirt:Clone()

		if not User.Character:FindFirstChild("Shirt") and not User.Backpack:FindFirstChild("Shirt") then
			ShirtTool.Parent = User.Character
		end
	end
end)

while true do
	-- Ensure the ShirtGenerator part exists in the Workspace
	local shirtGenerator = Workspace:FindFirstChild("ShirtGenerator")
	if not shirtGenerator then
		warn("ShirtGenerator part not found in Workspace.")
		return
	end

	-- Ensure the part to clone exists in ServerStorage
	local partToClone = ServerStorage:FindFirstChild("ShirtObject")
	if not partToClone then
		warn("PartToClone not found in ServerStorage.")
		return
	end

	-- Clone the part and set its position to the ShirtGenerator's position
	local clonedPart = partToClone:Clone()
	clonedPart.CFrame = shirtGenerator.CFrame
	clonedPart.Parent = Workspace

	-- Wait for a short duration before repeating
	task.wait(1)
end
1 Like

The shirt generating script and the proximity script are both inside serverscript service, and the shirt tool and the generated shirt are in serverstorage

Thank you I will try this script.

1 Like

That would be the cause of your problem of the Prompts not working.

local Pickup = workspace:WaitForChild("ShirtObject")

Pickup.ProximityPrompt.Triggered:Connect(function(player)
	local ShirtTool = game:GetService("ServerStorage").Shirt:Clone()
	
	if not player.Character:FindFirstChild("Shirt") and not player.Backpack:FindFirstChild("Shirt") then
		ShirtTool.Parent = player.Character
	end
end)

This code is only going to run once for the first object in the workspace it finds with the name “ShirtObject”. In order to do it this was, you’d need a different approach.

local Shirts = {}
local function newShirt(Shirt)
	Shirts[Shirt] = Shirt.ProximityPrompt.Triggered:Connect(function(player)
		local ShirtTool = game:GetService("ServerStorage").Shirt:Clone()

		if not player.Character:FindFirstChild("Shirt") and not player.Backpack:FindFirstChild("Shirt") then
			ShirtTool.Parent = player.Character
			Shirts[Shirt]:Disconnect()
			Shirt:Destroy()
		end
	end)
end

for _, Object in ipairs(workspace) do
	if Object. Name == "ShirtObject" then
		newShirt(Object)
	end
end
workspace.ChildAdded:Connect(function(Object)
	if Object.Parent == workspace and Object.Name == "ShirtObject" then
		newShirt(Object)
	end
end)
2 Likes

It is supposed to be a forever generating conveyor belt but once it goes through the other wall it gets deleted if not interacted with

1 Like

This one works amazingly! Thank you!

1 Like

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