Hello! I just found something called CollectionService and I’m trying to implement it into my game. But I ran into a roadblock.
It works on the first time, but on the second time it doesn’t. The Ui doesn’t show up for some reason.
-- // 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)
local player
part.ClickDetector.MouseClick:Connect(function(plr) -- This doesn't work on the second time
player = 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)
for _, btn in ipairs(ui:GetChildren()) do
if btn:IsA("TextButton") then
if btn.Name ~= "Exit" then
btn.MouseButton1Click:Connect(function()
local Character = player.Character or player.CharacterAdded:Wait()
local RootPart = Character.PrimaryPart
RootPart.Position = workspace.Teleportation:FindFirstChild(btn.Text).Position
ui.Parent = script
end)
else
btn.MouseButton1Click:Connect(function()
ui.Parent = script
end)
end
end
end
end
for _, Target in pairs(CollectionService:GetTagged("TeleportRooms")) do
LeaveRoom(Target)
end
Yes, I’ve tagged the parts I needed to tag. And yes, I made sure all the instances said weren’t nil. I have no idea what’s going on and I need some helping hand to debug.
It doesn’t work on the second time? what do you mean? like you click on part and it work but after you click other part it doesn’t work? if that is the case, try using a coroutine below. Example:
for _, Target in pairs(CollectionService:GetTagged("TeleportRooms")) do
local LeaveRoomCoroutine = coroutine.create(function()
LeaveRoom(Target)
end)
coroutine.resume(LeaveRoomCoroutine )
end
Exactly what I meant. And I tried using coroutines but it still doesn’t work. I also tried using task.spawn() and it also doesn’t work, so I’m assuming threads has nothing to do with this problem.
Now that I think about it, there is one error originating from the script. Though I already fixed the error (On the time this reply was posted) and it still doesn’t work.
local function LeaveRoom(part)
print("test1")
print(part)
local player
part.ClickDetector.MouseClick:Connect(function(plr) -- This doesn't work on the second time
print("test2")
player = plr
I cannot cut that out of the script, since that part of the script is the sole reason why I made this script in the first place. No, I cannot make it into a LocalScript, since I will need the teleportation to be server-sided rather than client.
That’s the point… PlayerAdded only runs when it’s firing an event or things alike. In this case, I’m using MouseClick and MouseButton1Click event. While the PlayerAdded doesn’t run infinitely, it runs when the event is fired. Also it got rid of the annoying error and it still teleported as intended, though just once.
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)
local player
for _, btn in ipairs(ui:GetChildren()) do
if btn:IsA(“TextButton”) then
if btn.Name ~= “Exit” then
btn.MouseButton1Click:Connect(function()
local Character = player.Character or player.CharacterAdded:Wait()
local RootPart = Character.PrimaryPart
part.ClickDetector.MouseClick:Connect(function(plr) – This doesn’t work on the second time
player = 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
end
end
for _, Target in pairs(CollectionService:GetTagged(“TeleportRooms”)) do
LeaveRoom(Target)
end
I
I don’t see anything useful that I could use to fix my script with this. You’re just posting my script but in a different order, which does nothing at all in this case.