Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make a brick that opens GUI at a certain distance?

while true do
wait(0.1)
local brick = script.Parent
local gui = brick.ShopGUI
local player = game.Players:GetChildren()
local distance = 15
for i=1,#player do
if ((brick.Position - player[i].Character.Torso.Position).magnitude>distance) then
local clone = gui:Clone()
clone.Parent = player.PlayerGui
brick.ShopGUI.Shop.Visible = false
elseif ((brick.Position - player[i].Character.Torso.Position).magnitude<distance) then
brick.ShopGUI.Shop.Visible = true
end
	end
end

Hello, thank you for reading. I have a problem. When I come near the brick, it does not spawn the GUI and the GUI's Frame called "Shop". If there is something wrong, can anyone please explain it to me? I would be very grateful for your help!

2 answers

✓ Accepted answer

Instead of magnitude use DistanceFromCharacter.

while wait() do --You can do while wait(.1) do instead of while true do wait(.1) and also, wait() is faster than wait(.1)
local brick = script.Parent
local gui = brick.ShopGUI
local player = game.Players:GetChildren()
local distance = 15
for i=1,#player do
if player[i]:DistanceFromCharacter(brick.Position) >= distance then
local clone = gui:Clone()
clone.Parent = player.PlayerGui
brick.ShopGUI.Shop.Visible = false
elseif player[i]:DistanceFromCharacter(brick.Position) <= distance then
brick.ShopGUI.Shop.Visible = true
end
    end
end

Hope this helps!

well...

you have to make a function.

function ShopQUI()

--fill in what you want to happen when the brick is touched by a player (The real thing that will let the brick react will be down here.)

brick.Touched:connect(ShopQUI) --brick is the object, if the brick is touched by a player the brick will . run the function ShopQUI.

To let it have a certain distance you will have to make an other part, as big as you want the distance to be. Make it Transparent (select Part, go to properties, Transparency has to be 1) and not CanCollide (select Part, go to properties, make sure the box at the left of CanColide is not checked.

This discussion is preserved as it was. New answers, comments, and votes are closed.