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

Auto close a gui when a player is to far away from a Part?

Asked by 5 years ago
Edited 5 years ago
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)

0
Is the player touching the part if it opens up? fr2013 88 — 5y
0
nope MageMasterHD 261 — 5y
0
it looks if the player is near enough (.magnitude) MageMasterHD 261 — 5y
0
Why not just put it in a local script to begin with instead of firing the client? Father_Odin 2 — 5y
0
I dont follow the issue with this script. Why cant you just use the same thing for every shop and say if you go X far from X shop trader then close? DinozCreates 1070 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

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

0
This does not work to open the GUI has already worked, however, I have to check if the gui of exactly this script will be opened to then synonymous only from this script to close MageMasterHD 261 — 5y
0
Oh okay. NeonProfile 111 — 5y
Ad

Answer this question