Team Changer In progress

TEAM CHANGER
*I am currently trying to script it so When i push M It pops up the UI, But I am having issues finding out how to make it work because I try to do the scripting but i think i got it wrong.

What can i do to script it To use M

2 Likes

You can use UserInputService or ContextActionService with Enum.KeyCode.M

where would i put this script in?

A localscript like you have, all input and ui related things go in a localscript

-- UserInputService
UserInputService.InputEnded:Connect(function(input, gameProcessedEvent)
    if input.KeyCode == Enum.KeyCode.M and not gameProcessedEvent then
       -- do the thing
    end
end)

So I make a local script then put it in the UI

You can put it anywhere, above is the code to run a function when the player presses M using UserInputService. You can also use ContextActionService, which could be better if the action should only be triggered in a certain context.


like this

yes, well you have to define UserInputService and write your code in it.

what kind of code? sorry Its a little confusing

The code to make your UI open

A basic code can look like this, but you have to make it so that it works in your code.

local UIS = game:GetService("UserInputService") -- reference to UserInputService Service
local NiceUI = "PutReferenceToNiceUIHere" -- reference to a Gui Object

local toggle = false -- just a toggle variable to know when it's open 

-- I use InputEnded if you want you can use InputBegan
UIS.InputEnded:Connect(function(input, gameProcessedEvent) -- UIS.InputEnded fires when the any input (such as key press) ends (like when key up) (InputBegan fires when the key is down)
    if input.KeyCode == Enum.KeyCode.M and not gameProcessedEvent then -- if the input is "M" and isn't a game processed event (such as typing in chat)
       if toggle == false then -- if the GUI is closed 
          NiceUI.Visible = true -- open 
          toggle = true -- set the variable to OPEN
       else
          NiceUI.Visible = false -- close
          toggle = false -- set the variable to CLOSE
       end
    end
end)

you have discord? Because Nice UI I have to put the ui name and then that would be all?

Yes, did you even try it? why don’t you try it and see what happens.

Also I’ll write comments in the code because you seem to not understand what it’s doing.

put the code in and wrote the GUI Name in the code testing

I tried it and it did not work? If you can contact in my dizzy so we can make this area not pack here my contact information.
Esprit de Corps#5980

GUI_toggle_example.rbxl (34.4 KB)

This works as expected.

As @batteryday said, just using input began instead. (Local Script) Place this script inside the UI you want to pop up.

local UIS = game:GetService("UserInputService")
local toggle = false

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.M then
		if toggle then
			script.Parent.Visible = false
			toggle = false
		elseif not toggle then
			script.Parent.Visible = true
			toggle = true
		end
	end
end)
1 Like

Yes I mentioned you can use InputBegan. It’s comes down to do you want the UI to open when the key is down or up.

InputBegan is probably better now that I think of it because if the user presses the key (down then up) there will be less latency

1 Like
local uis = game:GetService("UserInputService")
local toggle = false

uis.InputBegan:Connect(function(input, proc)
	if proc then
		return
	end
	if input.KeyCode == Enum.KeyCode.M then
		if not toggle then
			toggle = true
			script.Parent.Visible = true
		elseif toggle then
			toggle = false
			script.Parent.Visible = false
		end
	end
end)

I’ve added a little bit extra the variable “proc” is checking whether or not the input was processed by a core feature of the game itself, chat for example, as you wouldn’t want an input to trigger the UI opening/closing if the input was simply a user typing the character “M” while chatting.