Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why is GUI only showing for one part when magnitude is low enough?

Asked by 4 years ago

I am currently writing a script that shows a GUI when a player is less than 10 studs away using magnitude. It only works for one of the doors when I loop through the Doors folder in workspace.

local Doors = workspace:WaitForChild("Compound-8"):WaitForChild("Doors")
local plr = game.Players.LocalPlayer
local ConfirmationTrue = plr.PlayerGui:WaitForChild("Interactions"):WaitForChild("Door"):WaitForChild("DoorConfirmationTrue")
local DoorInteraction = plr.PlayerGui:WaitForChild("Interactions"):WaitForChild("Door"):WaitForChild("DoorInteraction")
local RunService = game:GetService("RunService")

wait(2)


RunService.Heartbeat:Connect(function()
    local found
    for i,v in pairs(Doors:GetChildren()) do        
            local char = plr.Character
            local magnitude = (v.Door.Position - char:WaitForChild("Head").Position).Magnitude
        if magnitude <= 10  then
            found = true
            if ConfirmationTrue.Visible == false then
                if found == true then
                    DoorInteraction.Visible = true
                end
                end
            end
        if magnitude >= 10  then
            found = false
            DoorInteraction.Visible = false
        end
    end
end)




Answer this question