Help with CollectionService

Unfortunately I cannot give out my game file just like that. You could just try to recreate the scenario in Roblox Studio on your own.

1.if it works what it will work
2.what’s the issue i still didn’t got it touch event doesn’t work or what
3.what does it about ui tp part?

if you could record it it will help a lot or explain what its process should be

  1. I don’t understand what you’re trying to say on this bit
  2. So I was making a script, it’s supposed to teleport the player to a different point in the map, on the first attempt it worked flawlessly but after I tried it again with a different basepart that’s already been tagged, it doesn’t work
  3. Teleports the player to a different point in the map when a button is clicked in a ui which the server gave to the player

It’s random tp or specific tp?

Teleports to a point on the map marked by a basepart

Hi, I hope this will work:

-- // Services \\ --

local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")
local CollectionService = game:GetService("CollectionService")

-- // Elements \\ --

local uiTemplate = script.Location

-- // Interact \\ --

local function LeaveRoom(part)
	Players.PlayerAdded:Connect(function(player)
		local p = nil
		local ui = uiTemplate:Clone() -- More uis for more players
		
		Players.PlayerRemoving:Connect(function(plr)
			if player == plr then
				ui:Destroy() -- Not needed anymore because player left
			end
		end)
		
		part.ClickDetector.MouseClick:Connect(function(plr) -- This doesn't work on the second time
			if plr == player then
				p = plr
				if plr:DistanceFromCharacter(part.Position) <= 10 then
					local DarkFrame = plr.PlayerGui:FindFirstChild("DarkFrame")

					ui.Parent = plr.PlayerGui

					for i = 1, 4 do
						local Option = part:FindFirstChild("Options"):GetChildren()
						ui[tostring(i)].Text = Option[i].Value
					end
				end
			end
		end)

		for _, btn in ipairs(ui:GetChildren()) do
			if btn:IsA("TextButton") then
				if btn.Name ~= "Exit" then
					btn.MouseButton1Click:Connect(function()
						if p == player then
							p = nil
							local Character = player.Character or player.CharacterAdded:Wait()
							local RootPart = Character.PrimaryPart

							RootPart.Position = workspace.Teleportation:FindFirstChild(btn.Text).Position
							ui.Parent = script
						end
					end)

				else
					btn.MouseButton1Click:Connect(function()
						if p == player then
							p = nil
							ui.Parent = script
						end
					end)
				end
			end
		end
	end)
end

for _, Target in pairs(CollectionService:GetTagged("TeleportRooms")) do
	LeaveRoom(Target)
end

And what is DarkFrame ?

And what is DarkFrame?

DarkFrame is a Ui that’s used to make a sort of smooth transition upon teleporting

-- // Services \\ --

local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")
local CollectionService = game:GetService("CollectionService")

-- // Elements \\ --

local uiTemplate = script.Location

-- // Interact \\ --

local function LeaveRoom(part)
	Players.PlayerAdded:Connect(function(player)
		local p = nil
		local ui = uiTemplate:Clone() -- More uis for more players
		
		Players.PlayerRemoving:Connect(function(plr)
			if player == plr then
				ui:Destroy() -- Not needed anymore because player left
			end
		end)
		
		part.ClickDetector.MouseClick:Connect(function(plr) -- This doesn't work on the second time
			if plr == player then
				p = plr
				if plr:DistanceFromCharacter(part.Position) <= 10 then
					local DarkFrame = plr.PlayerGui:FindFirstChild("DarkFrame")

					ui.Parent = plr.PlayerGui

					for i = 1, 4 do
						local Option = part:FindFirstChild("Options"):GetChildren()
						ui[tostring(i)].Text = Option[i].Value
					end
				end
			end
		end)

		for _, btn in ipairs(ui:GetChildren()) do
			if btn:IsA("TextButton") then
				if btn.Name ~= "Exit" then
					btn.MouseButton1Click:Connect(function()
						if p == player then
							p = nil
							local Character = player.Character or player.CharacterAdded:Wait()
							local RootPart = Character.PrimaryPart

							RootPart.Position = workspace.Teleportation:FindFirstChild(btn.Text).Position
							ui.Parent = script
						end
					end)

				else
					btn.MouseButton1Click:Connect(function()
						if p == player then
							p = nil
							ui.Parent = script
						end
					end)
				end
			end
		end
	end)
end

for _, Target in pairs(CollectionService:GetTagged("TeleportRooms")) do
	LeaveRoom(Target)
end

The script did not work, also it is not needed to be cloned as my game will be a singleplayer game and not multiplayer. Also I don’t really see the use of the variable “p” since player is already specified as a parameter in PlayerAdded

Hey!


Are you sure that the event does not fire after the first time?

I’m asking this because I have tried to recreate your problem with a model named Detectors located in the workspace that has some parts named differently and seems like it works fine and prints the part name.
There was another server script which has tagged the model parts, too.

Here is the script which I’ve cut its gui part:

local print = function(...)
	print("Something -", ...)
end

-- // Services \\ --

local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")
local CollectionService = game:GetService("CollectionService")

-- // Elements \\ --

local ui = workspace.Detectors

-- // Interact \\ --

local function LeaveRoom(part)
	local player
	part.ClickDetector.MouseClick:Connect(function(plr) -- This doesn't work on the second time
		player = plr
		print("Clicked -", part.Name)
		
		-- ...
	end)
	-- ...
end

for _, Target in pairs(CollectionService:GetTagged("TeleportRooms")) do
	LeaveRoom(Target)
end

Model:
image

Prints:
image

Are you sure that the event does not fire after the first time?

I am certain that the script does not work after the first attempt. However, after taking a look at your script, I’ve gotten an idea. I will get back to you once I’m done

EDIT: I’ve found the core of the issue and I’m working on it

It turns out my script was fine and all, there was just one small mistake though. I never knew that moving the RootPart of the Character alone wouldn’t bring the other bodyparts along with it, so the :DistanceFromCharacter registers that the other bodyparts aren’t within the distance specified so it didn’t execute the rest of the script. Thanks to everyone who tried to help

Updated Version:

-- // Services \\ --

local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")
local CollectionService = game:GetService("CollectionService")

-- // Elements \\ --

local ui = script.Location

-- // Interact \\ --

local function LeaveRoom(part)
	Players.PlayerAdded:Connect(function(plr)
		
		part.ClickDetector.MouseClick:Connect(function()
			print("Test1")
			if plr:DistanceFromCharacter(part.Position) <= 10 then
				print(plr:DistanceFromCharacter(part.Position))
				local DarkFrame = plr.PlayerGui:FindFirstChild("DarkFrame")

				ui.Parent = plr.PlayerGui

				for i = 1, 4 do
					local Option = part:FindFirstChild("Options"):GetChildren()
					ui[tostring(i)].Text = Option[i].Value
				end
			end
		end)

		for _, btn in ipairs(ui:GetChildren()) do
			if btn:IsA("TextButton") then
				if btn.Name ~= "Exit" then
					btn.MouseButton1Click:Connect(function()
						local Character = plr.Character or plr.CharacterAdded:Wait()

						for _, Parts in pairs(Character:GetChildren()) do
							if Parts:IsA("BasePart") or Parts:IsA("MeshPart") then
								Parts.Position = workspace.Teleportation:FindFirstChild(btn.Text).Position
							end
						end
						ui.Parent = script
					end)

				else
					btn.MouseButton1Click:Connect(function()
						ui.Parent = script
					end)

				end
			end
		end
		
	end)
end

for _, Target in pairs(CollectionService:GetTagged("TeleportRooms")) do
	LeaveRoom(Target)
end

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