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

How do you make a brick open a gui when touched?

Asked by
Yeevivor4 155
9 years ago
StarterGui = game.StarterGui
function onTouched(hit)
StarterGui.ShopGUI.ShopFrame.Visible = true
StarterGui.ShopGUI.Open.Visible = false 
end
connection = script.Parent.Touched:connect(onTouched)

Hi, thank you for reading. What I am trying to do is to open a simple GUI when you touch a brick and it closes when you leave the brick. I don't know much about GUIs, and would love some assistance.

2 answers

Log in to vote
0
Answered by 9 years ago

You could clone the GUI to the player's playergui, and then destroy it once the player leaves it.

--Assuming the GUI and script are within the brick:

local sP = script.Parent
local gui = sP.ShopGUI
local v = sP.CFrame:vectorToWorldSpace(Vector3.new(sP.Size.X/2,sP.Size.Y/2,sP.Size.Z/2))

--Pretend v is equal to a Vector3 value that represents the size of the script's parent divided by two
--I used vectorToWorldSpace because it tracks rotation, so the part might have a better chance on being more accurate with a slight rotation. You don't need to use vectorToWorldSpace

sP.Touched:connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent) --returns a player, if it exists.
if player and not player.PlayerGui:FindFirstChild("ShopGUI") then --checks to see if the GUI already exists.
local clone = gui:Clone()
clone.Parent = player.PlayerGui
while wait(1) do

--Here I am comparing the position of the player's torso to the position of the script's parent and it's size. 

--For the first line, I am checking to see if the x value of the torso's position is less than or equal to the x value of position of the script's parent plus the x value of it's size divided by two. If it returns true, then it means that the player's Torso is to the left of the part's right side on the x axis. The rest of the lines follow a similar pattern.

--If you have learned about linear inequalities, this is very similar. I am checking to see if a point is within a position where all of the lines present a possibility.

--"not" after if negates the value of the inside of the parenthesis, so that the code is only called when the player is outside of the part.


if player.PlayerGui:FindFirstChild("ShopGUI") and  --checks to see if the GUI exists, to be safe.

not(player.Character.Torso.Position.x <= sP.Position.x + v.x and
player.Character.Torso.Position.x >= sP.Position.x - v.x and

player.Character.Torso.Position.y <= sP.Position.y + v.y + 18.5 and --18.5 is an offset, so that if the player jumps above the part then it will not delete the gui.

player.Character.Torso.Position.y >= sP.Position.y - v.y and --You could probably remove this line, unless you want the part to be no-collide.

player.Character.Torso.Position.z <= sP.Position.z + v.z and
player.Character.Torso.Position.z >= sP.Position.z - v.z)
then
--if any of the lines within the code above return false, then the if statement will receive true and destroy the existing GUI.
clone:Destroy()
end
end
end
end)

This might not work as well within rotated parts.

0
Wow, you did it. But, something I was confused about is all the Torso.Positioning. can you explain a little of that to me? Yeevivor4 155 — 9y
0
How's that? aquathorn321 858 — 9y
0
I wouldn't rely on TouchEnded, by the way. If you walk on the part, then it will repeatedly destroy and clone the gui, and it's likely that roblox won't recognize when the touch is ended and the gui will be stuck on your screen. aquathorn321 858 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

This is more simple than the other question. If you don't want the gui to disappear when they jump, just resize the part. This is VERY SIMPLE! It doesn't do anything with the player's torso position.


You can use the Touched event to see if the play touched it:

script.Parent.Touched:connect(function(part)
    print("A part named "..part.Name.." has touched me!")
end)

The output would say

A part named part has touched me! A part named part has touched me! A part named part has touched me! A part named part has touched me! A part named part has touched me!

That is because there is no debounce to make it work once. This is it with a debounce:

local debounce = false

script.Parent.Touched:connect(function(part)
    if not debounce then
        print("A part named "..part.Name.." has touched me!")
    end
end)

The output will be:

A part named part has touched me!


Did you know that Touched event has a brother? It's called TouchEnded. As you can guess TouchedEnded fires when something leaves the part.

script.Parent.TouchEnded:connect(function(part)
  print(part.." has left this BasePart :C")
end)

OutPut:

Part has left this BasePart :C


Now we add all this together, but first we need to see what the family tree must be like.

Part in workspace, Script in Part, Gui in Script.

script.Parent.Touched:connect(function(char)
    local player = game.Players:GetPlayerFromCharacter(char)
    if player and not player.PlayerGui.StoreGui then --and not player is the debounce.
        script.StoreGui:clone().Parent = player.PlayerGui
    end
end)

script.Parent.TouchEnded:connect(function(char)
    local player = game.Players:GetPlayerFromCharacter(char)
    if player and player.PlayerGui.StoreGui then
        player.PlayerGui.StoreGui:Destroy()
    end
end)

Here is the link to Destroy and Clone.

I hope this helps.

PS: I am aware that there is another answer. I just answered anyway since you seemed confused.

Answer this question