I am attempting to create a Script that uses CollectionService for the script to be replicated into a multitude of tagged doors.
The only problem that is occurring is updating String Value for when the doors status is changed (aka open or closed). The string value determines the text of a TextLabel that is locked to the cursor, showcasing the action that can be proceeded.
Code Snippet:
local CollectionService = game:GetService("CollectionService")
for i, PublicDoor in pairs(CollectionService:GetTagged("DoorPublic")) do
local Door = PublicDoor.Door
local DoorFront = Door.DoorFront
local DoorBack = Door.DoorBack
local DoorTargetFront = Door.Parent.DoorTargetFront.CFrame
local DoorTargetBack = Door.Parent.DoorTargetBack.CFrame
local DoorTargetClose = Door.Parent.DoorTargetClose.CFrame
local isOpen = false
function updateTextLabel(text)
DoorFront.SelectionInfo.TextValue.Value = text
DoorBack.SelectionInfo.TextValue.Value = text
end
updateTextLabel("Open Door")
DoorBack.ClickDetector.CursorIcon = "rbxassetid://16936360672"
DoorFront.ClickDetector.CursorIcon = "rbxassetid://16936360672"
DoorFront.ClickDetector.MouseClick:Connect(function()
if isOpen then
Door:PivotTo(DoorTargetClose * CFrame.Angles(0,math.rad(180),0))
Door.door_close:Play()
isOpen = false
updateTextLabel("Open Door")
else
Door:PivotTo(DoorTargetFront)
Door.door_open:Play()
isOpen = true
updateTextLabel("Close Door")
end
end)
DoorBack.ClickDetector.MouseClick:Connect(function()
if isOpen then
Door:PivotTo(DoorTargetClose * CFrame.Angles(0,math.rad(180),0))
Door.door_close:Play()
isOpen = false
updateTextLabel("Open Door")
else
Door:PivotTo(DoorTargetBack * CFrame.Angles(0,math.rad(180),0))
Door.door_open:Play()
isOpen = true
updateTextLabel("Close Door")
end
end)
end
From the video, you can see that only the left most door’s text updates, while the others do not.
for i, PublicDoor in pairs(CollectionService:GetTagged("DoorPublic")) do
local Door = PublicDoor.Door
local DoorFront = Door.DoorFront
local DoorBack = Door.DoorBack
local DoorTargetFront = Door.Parent.DoorTargetFront.CFrame
local DoorTargetBack = Door.Parent.DoorTargetBack.CFrame
local DoorTargetClose = Door.Parent.DoorTargetClose.CFrame
local isOpen = false
what you did was, instead of using PublicDoor.DoorBack, you used Door.DoorBack, which broke the script.
When you do the for Index, Value do loop and want each to take info from each door, you would use the Value, because that’s what the for i loop does.
for i, PublicDoor in pairs(CollectionService:GetTagged("DoorPublic")) do
local Door = PublicDoor.Door
local DoorFront = PublicDoor.DoorFront
local DoorBack = PublicDoor.DoorBack
local DoorTargetFront = PublicDoor.Parent.DoorTargetFront.CFrame
local DoorTargetBack = PublicDoor.Parent.DoorTargetBack.CFrame
local DoorTargetClose = PublicDoor.Parent.DoorTargetClose.CFrame
local isOpen = false
Without local it makes the function “global”. The left door is probably the last index of CollectionService:GetTagged("DoorPublic") and therefore overrides the updateTextLabel() function. I don’t know if you noticed, but if you clicked on a door besides the left one, it would update the left door’s values. local specifies that this function can only be used within the scope and therefore does not override the other functions in different scopes with the same name (updateTextLabel).