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

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

Asked by
Yeevivor4 155
9 years ago
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

Log in to vote
1
Answered by 9 years ago

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!

Ad
Log in to vote
-1
Answered by 9 years ago

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.

Answer this question