So, this script is supposed to print that a Player is close to a gate when they are within 6 studs. But, for some reason, it does not do so, even when they are within the specified amount of studs. Can you help me understand why it is not working?
-----------------
--| Variables |--
-----------------
local Players = game:GetService("Players")
local Gates = script.Parent:GetChildren()
local RequiredDistance = 6
-----------------
--| Functions |--
-----------------
function GetDistance(Object)
if (Object:IsA("Player")) then
for _,Child in pairs(Gates) do
if (Child:IsA("Part")) then
local Distance = Object:DistanceFromCharacter(Child.Position)
if (Distance <= RequiredDistance) then
return true
end
end
return false
end
end
end
-----------------
--| Main Code |--
-----------------
Players.PlayerAdded:connect(function(Player)
local Character = Player.Character or Player.CharacterAdded:wait()
repeat wait() until Character.Torso
local Torso = Character.Torso
Torso.Changed:connect(function()
if (GetDistance(Player)) then
print(Player.Name.." is close to a gate!")
end
end)
end)

1 comment
Wow... This was an extremely helpful and constructive answer. Honestly, one of the best I've ever gotten and seen. Thank you very, very much for your help!