I’ve rewritten the same piece of code at least 4 times now and i keep having the same problem every single time.
I’m trying to use collectionservice for all the elements of my puzzle game so i can run the same code for every door, button etc. However, whatever I do, if there are two instances with the same tag only one of them will function. Every time I rewrite this. I’ve looked up this service several times now and I always get the same result.
I don’t know what I’m doing wrong. From every tutorial I’ve read, I’m not doing anything wrong anyway.
Here is my latest attempt which doesn’t work:
local CollectionService = game:GetService("CollectionService")
local tags = {door = 'door_exit'}
local function setDoorState(door, state)
for _, b in pairs(door.Positions:GetChildren()) do
if b:IsA('BasePart') then b.Transparency = 1 end
end
door.Components.Collision.Transparency = 1
for _, part in pairs(door.Lights:GetChildren()) do
if part:IsA('BasePart') then
part.Color = (state and Color3.fromRGB(91, 154, 76) or Color3.fromRGB(255, 55, 55))
end
end
game.ReplicatedStorage.Animate.AnimateChamberDoor:FireAllClients(door, state, door.Configuration.OpeningTime.Value)
door.Components.Collision.CanCollide = not state
end
function doorHandling(door)
spawn(function()
running = true
config = door.Configuration
state = false
if config.Requirement.Value then
spawn(function()
while door and running do
wait()
if door and config.Requirement.Value then
enabled = config.Requirement.Value.Configuration.Enabled.Value
if state ~= enabled then
state = enabled
setDoorState(door, state)
end
else
break
end
end
end)
else
spawn(function()
while running and door do
wait()
local open = false
for _, plr in pairs(game.Players:GetPlayers()) do
local character = plr.Character
if character and character.Humanoid.Health > 0 then
local distance = (door.Positions.CentralPoint.Position - character.HumanoidRootPart.Position).magnitude
if distance <= config.MinimumDistance.Value then
open = true
break
end
end
end
if state ~= open then
state = open
setDoorState(door, state)
end
end
end)
end
end)
end
local function updateTaggedObjects()
local taggedParts = CollectionService:GetTagged('door_exit')
for _, door in pairs(taggedParts) do
doorHandling(door)
end
end
updateTaggedObjects()
What I’m trying to do is have a door that has a config folder that allows me to adjust things such as the speed the door opens, the minimum distance a player needs to be to open automatically and whether or not the door requires a trigger e.g. a button to be enabled to be able to open.
The RemoteEvent is just for client side animations. It doesn’t change anything else.
It works, but only for one door, one button etc. Whenever there is more than one object with the same tag as another one, it will be ignored for some reason. Only one object per tag will function.
Here is my door layout:
Can someone with better knowledge of CollectionService and OOP style programming PLEASE help me out here, I’m getting frustrated with this and it’s hindering my entire game development.