Help on scripting

I’ve been looking forward on learning how to make a “Press Button from keyboard” gui where basically it’ll do something once the button is pressed, the idea is when I get close to something like a switch or button it’ll appear a gui and I will need to press a certain button in my keyboard, anyone have any idea on how to do that?

1 Like

you would do something like this

local UIS = game:GetService("UserInputService")
local Frame = game.Players.LocalPlayer.PlayerGui.ScreenGui.Frame
Frame.Visible = false --instead of this you can make Frame Invisible
UIS.InputBegan:Connect(function(Keycode)
        if Keycode.KeyCode == Enum.KeyCode.E then -- E is the keybind,
             Frame.Visible = true
   end)
UIS.InputEnded:Connect(function(Keycode)
        if Keycode.KeyCode == Enum.KeyCode.E then -- E is the keybind,
             Frame.Visible = false
   end)

Where exactly that script will go into?

Doesn’t accomplish what OP wants. They’re looking for a proximity-based activator, not a hold Gui.

Ideally this is something you want to accomplish through the use of UserInputService andposition checks between the character’s root and your target interaction item. All of this should be done locally in a script under PlayerScripts (contents are filled from StarterPlayerScripts upon spawn).

Have you attempted this problem yourself first, searched for what you may need or defined your requirements before asking the question? Scripting support is typically for help with broken code or concepts, not for abstract problems wherein you don’t know how to accomplish something. As far as searching goes, there are many topics about distance-based activators around here on the DevForum.

There was a similar post made a while back.

Here you go

1 Like

Can you only put a Frame in a ScreenGui?

into StarterPlayerScript as you like or was it startercharacterscriptss

no you can alot more into Screengui

You would put the following in a LocalScript inside the PlayerGui or StarterPlayerScripts:

local runs, uis = game:GetService("RunService"), game:GetService("UserInputService")
local plr = game.Players.LocalPlayer
local char = plr.CharacterAdded:Wait()
local frame = plr.PlayerGui.ScreenGui.Frame

local maxDistance = 15
local canClick = false

runs.RenderStepped:Connect(function()
	if char then
		local charDistance = (char.HumanoidRootPart.Position - workspace.Switch.Position).magnitude
		if charDistance < maxDistance then
			canClick = true
			frame.Visible = true
		else
			canClick = false
            frame.Visible = false
		end
	end
end)

uis.InputBegan:Connect(function(input, gameProcessed)
	if input.KeyCode == Enum.KeyCode.E then
		if canClick == true then
			print("do something")
		end
	end
end)

What this does is check the players distance and set a variable based on whether they’re close enough or not. Then there is a function firing when the E key is pressed that checks if the canClick variable is true. If it is, then it prints “do something”: which you would replace with whatever you want the switch to do.

1 Like

What I ended up doing to solve this was to save a local variable on the player’s client called target. Then when I touch the activation area, I set target to that part and show the gui on it (setting its Adornee). Any time the player presses F (or whatever) I make sure target exists and then fire that over to the server to check proximity. I also created a looping function to check for distance on the client side to know when to remove the gui and target value once the target has been added. It ended up looking pretty good!

(Please ignore the lack of animation transition at the moment lol)
https://gyazo.com/eeb8890bc6e6f6b5237fb1ad48215cba

2 Likes