Okay, the title is a bit goofy. What I’m trying to achieve is a bit too complex for me to fit into a short title.
First of all, I really want to make my teleportation script shorter for me to deal with. before it’d require billions of lines of repeating code. the sight was nasty, so I came up with a better idea which is basically making the script count all the teleportation parts I have and add them to every teleportation thingy and such… its very hard to explain ,but I’m sure you’re getting the idea?
things aren’t going very smoothly as I’m not very smart when it comes to complex code. i got stuck and can’t figure out how i would make findfirstchild work inside a table, and how to make the code work in general…
can someone point me in the right direction? I’d appreciate a bit of assistance. you don’t have to write any scripts, just tell me what I should look into and what parts are bringing me script issues. thank you!
here is the script (note: i left some parts out which do not affect the part of script im showing here.)
local count = 0;
local rooms = workspace.Rooms:GetChildren()
local descendant = rooms:FindFirstChild("prox")
local evil = descendant:GetChildren()
--//code
evil.Triggered:Connect(function(player)
for _, i in ipairs(evil) do
count = count + 1
if evil.Name == ("promptObject" ..count) then
RemoteEvent:FireClient(player, "Teleport" ..count)
end
end
end)
print("We have", count ,"teleports in the system right now")
You should iterate through all the rooms, then have the .Triggered code run for each one:
local count = 0;
local rooms = workspace.Rooms:GetChildren()
for _, room in ipairs(rooms) do
local descendant = room:FindFirstChild("prox")
local evil = descendant:GetChildren()
evil.Triggered:Connect(function(player)
for _, i in ipairs(evil) do
count = count + 1
if evil.Name == ("promptObject" ..count) then
RemoteEvent:FireClient(player, "Teleport" ..count)
end
end
end)
print("We have", count ,"teleports in the system right now")
end
thank you for your help, but unfortunately the situation wasn’t improved by a lot. this did help me get past the findfirstchild table issue, but now I am faced with getchildren index nil. I did fiddle around the code and try to fix the issue but I was only faced by other issues I’ve been through before. I’ll continue looking around and see if there’s something I missed.
You should make use of the CollectionService class to handle many prompts under a single instance tag.
A tag is a string which you attach to an instance so that we can refer to them later.
Now that you understand how collection service works, lets try to implement it:
for i: number, v: Instance in ipairs(CollectionService:GetTagged("PromptTag")) do
if (v:IsA("ProximityPrompt")) then
v.Triggered:Connect(function(Player: Player): ()
--// implement your logic
end);
end;
end;
I missed another case where you had to iterate. Try the updated code below:
local count = 0;
local rooms = workspace.Rooms:GetChildren()
for _, room in ipairs(rooms) do
local descendant = room:FindFirstChild("prox")
local evil = descendant:GetChildren()
for _, child in ipairs(evil) do
child .Triggered:Connect(function(player)
count = count + 1
if evil.Name == ("promptObject" ..count) then
RemoteEvent:FireClient(player, "Teleport" ..count)
end
end
end)
print("We have", count ,"teleports in the system right now")
end
oh yay! thank you! i had used collectionservice once before, but i had forgotten it existed! what an useful tool it is!
this helped to fix my issue. thank you greatly glaphyre and PoppyandNeivaarecute for helping me.
here is the script i ended up with, for those that might have the same problem as i have:
(the last character of proximityprompt must have a number that matches with its teleport event)
for i: number, v: Instance in ipairs(CollectionService:GetTagged("telepa")) do
if (v:IsA("ProximityPrompt")) then
count = count + 1
v.Triggered:Connect(function(Player: Player): ()
local finalnumber = v.Name:sub(#v.Name, #v.Name)
RemoteEvent:FireClient(Player, "Teleport" ..finalnumber)
end);
end;
end;
print("We have", count ,"teleports in the system right now")