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

How to destroy gui if player gets at a certain distance from a brick?

Hello, first of all I want to say that I'm not really skilled in scripting, I'm trying to learn day by day to develop my game. Recently I built a vending machine and I managed to get it working. The GUI pops up if a player clicks on the vending machine and they can buy items by spending money. All fine for now.

How can I make a script that manages to destroy the GUI if the player gets far from the vending machine?

Really thanks for helping, I don't really know what to do.

1 comment

0

You could do if they walk on a part then it would destroy it (you would put the part where you want). That would make it, so you can remove it easily. Btw, you would probably be using a local script in the part.

2 answers

✓ Accepted answer

You can use the .Magnitude property of a Vector subtraction outcome to calculate the given distance between two provided Positions. By combing this with RunService's .RenderStepped signal, we can perpetually verify the given distance has exceeded before the next frame is rendered

Upon this condition proving true, we can access the PlayerGui container which is used to handle the User Interface, and locate the GUI we wish to remove.


Note:

I suggest you set the .Parent property of an Instance to nil if you wish to remove it but use it again—as expected from getting closer to the Vending Machine, it should appear again. Calling :Destroy() on the Instance will eradicate the Instance from PlayerGui until replaced; you'll have to repeated add the Instance back if so, it will become tedious.

However, since this is a ScreenGui Instance I presume, we can simply just set Enabled to false.


local runService = game:GetService("RunService")

local player = game.Players.LocalPlayer

local part1 = workspace.Part1
local part2 = workspace.Part2
local partDistance = (Part1.Position - Part2.Position).magnitude

local maxDistance = 10 --// Studs

runService.RenderStepped:Connect(function()
	if (partDistance > maxDistance) then
		local playerGui = player:FindFirstChild("PlayerGui")
		if (playerGui and playerGui.VendingGui) then
			playerGui.VendingGui.Enabled = false
		end
	end
end

If this helped, please don't forget to accept this answer!

1 comment

0

It worked, thanks :)

you can use a part and check touchended or you can use AnyPlayer:DistanceFromCharacter(any vector3)

heres what that looks like on a client sided script (localscript):

didnt test yet edit: tested and works edit 2: noticed this is a bad method of doing this, you probably shouldnt use this code

local plr = game:GetService("Players").LocalPlayer
local checkOn = workspace.??? -- Replace this to the part you want to check if the player is to far from
local gui = ??? -- change this to the gui you will destroy upon being to far from the part from the variable checkOn
local maxDistance = 6 -- maximum distance from vending machine

game:GetService("RunService").RenderStepped:Connect(function()
if plr:DistanceFromCharacter(checkOn.Position) >= maxDistance then
gui:Destroy()
end
end)

4 comments

0

Thanks, I'll test it later and see if it works

0

no problem :)

0

really bad way of doing it, the comment on the question is perfect and cant really be exploited as easily, this is so easily exploited

0

Gameplayer365247v2, sorry for doing a bad method, your right

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