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

How To Make A Clickable GUI When Looking At Part?

Asked by 10 years ago

Hi I'm trying to create a script for doors in my game so that when the player looks at them a text button will appear saying "Open". When said Gui is clicked the door will open. Just as well, when the door is open the Gui will say "Close". Here's the code I currently have:

mouse.Button1Down:connect(function()
    if mouse.Target == workspace.TestDoor 
    and player:DistanceFromCharacter(mouse.Target.Position)<15 then
        if mouse.Target.Clicked.Value == true then
            mouse.Target.Clicked.Value = false
        else mouse.Target.Clicked.Value = true
        end
    end
end)

So what's happening here is that I have a test door named "TestDoor". When the mouse clicks the script checks if that target is specifically TestDoor and that the player is at least 15 units away from it. If all this is true then there is a value within TestDoor called Clicked which is changed. I have a script within TestDoor that does this to make sure the previous script is working:

while true do
    wait(2)
    print(script.Parent.Clicked.Value)
end

Also, the first script is placed in the players backpack which I'm not sure is the proper area. I've done a lot of programming in the past but am new with scripting for Roblox.

As for the gui part I've tried things like this but to no avail:

if mouse.Target   == workspace.TestDoor and 
player:DistanceFromCharacter(mouse.Target.Position)<15 then
    local screen  =  Instance.new("ScreenGui")
    screen.Parent =  player.PlayerGui
    local button  =  Instance.new("TextButton")
    button.Parent =  screen
end

So I'm at a bit of a loss when it comes to detecting when the player is looking at the door from a specific distance away. Just a side note: ClickDetectors are out of the question since they don't have the functionality I require. More specifically you cannot tell whether someone right or left clicked with a click detector.

Anyway, if someone could point me in the right direction on how I should go about doing this it would be greatly appreciated. Thanks!

Answer this question