Hi, I have tried a few things but im blanking right now, how would I get this to work with CollectionService in a way that it would work with every button that has been tagged?
Right now it is only working with just one.
-- Checking distance to determine if open or close
local function checkDistance(button)
print(button)
while task.wait(0.5) do
local withinDistance = player:DistanceFromCharacter(button.Main.CFrame.p) <= 15
if withinDistance then
if not button.Main:FindFirstChild("ButtonInfoUI") then
openButtonUI(button, buttonUI)
end
elseif button.Main:FindFirstChild("ButtonInfoUI") then
closeButtonUI(button, buttonUI)
end
end
end
-- Loop through buttons
for _, button in pairs(collectionService:GetTagged("Buttons")) do
checkDistance(button)
end
The reason this is happening is because the function is in an infinite loop, which is pausing the iteration of the buttons when the function gets called. Put the while loop in the checkDistance() function inside a task.spawn(). Or, while iterating through the tagged buttons, put the calling of the function checkDistance() inside a task.spawn()
Examples:
local function checkDistance(button)
print(button)
task.spawn(function()
while task.wait(0.5) do
local withinDistance = player:DistanceFromCharacter(button.Main.CFrame.p) <= 15
if withinDistance then
if not button.Main:FindFirstChild("ButtonInfoUI") then
openButtonUI(button, buttonUI)
end
elseif button.Main:FindFirstChild("ButtonInfoUI") then
closeButtonUI(button, buttonUI)
end
end
end)
end
for _, button in ipairs(CollectionService:GetTagged("Button")) do
task.spawn(checkDistance)
end