local MaxDistanceToShop = 24 local id = script.Parent.ShopID.Value wait () while true do wait () local Player = game.Workspace["Main System"].Players:GetChildren() for _,v in pairs (Player) do if (script.Parent.Trader.Position - v.UpperTorso.Position).magnitude <= MaxDistanceToShop then local Player = game.Players:FindFirstChild(v.Name) if Player then game.Workspace["Main System"].Events.OpenShopEvent:FireClient(Player,id) end end end end
With this script i can look Shop sided if the player is near enough to buy something but how whould you do this to look if a player is too far away to auto close the shop?
(The problem is there are more than 1 shop so i can't just do : magnitude <= MaxDistanceToShop
because it would close the shop instant if it gets opened because you can't be near enough at all shops)
A common mistake, you don't know if you're position magnitude would be negative, consider using this:
I don't know if this will solve your problem, but it's the best thing that has a tiny hint of working, thanks.
local MaxDistanceToShop = 24 local id = script.Parent.ShopID.Value wait () while true do wait () local Player = game.Workspace["Main System"].Players:GetChildren() for _,v in pairs (Player) do if math.abs((script.Parent.Trader.Position - v.UpperTorso.Position).magnitude) <= MaxDistanceToShop then local Player = game.Players:FindFirstChild(v.Name) if Player then game.Workspace["Main System"].Events.OpenShopEvent:FireClient(Player,id) end end end end