UserInputService doesn't detect button presses across multiple scripts

I’m trying to make an FPS game with a custom movement system and a gun system that doesn’t use Roblox tools.

I have 2 different scripts that use UserInputService: the movement script and the gun script. I found that when you hold W and Shift using the movement script, you cannot switch weapons by pressing 1 or 3 using the weapon script.

I also have 2 scripts that use RunService events: movement script using Heartbeat, viewmodel script using RenderStepped. Those could be conflicting but I don’t have enough knowledge/experience with Roblox scripting to answer that.

Basically when one script uses UserInputService the other one isn’t able to pick up input until the other input is stopped.
How do I fix this?

Thanks in advance!

In this case there is no conflict, as you can have several of these similar events operating in parallel.

Correct. In this case, since you need to operate a simultaneous key combination, you will have to have a UserInputService in a unique script.
And if you need to refer to the same UserInputService in both scripts, move the UserInputService to a ModuleScript.

What do you mean to have UserInputService in a unique script? (sorry never heard of that)
And do I just connect both scripts to a ModuleScript to fix the problem?
(I’ve never worked with module scripts so idk how they work but if that’s the solution i’ll try)

You don’t need multiple scripts.
I only use 3:

  1. LocalScript
  2. ServerScript
  3. ModuleScript
    And the first two use the same ModuleScript

In this way, it is easier to control all the behavior of the game, in addition to avoiding these confusions, like the one you had.

I kinda get what you’re saying but I’ve never worked with Module Scripts. Would I need to catch the user input on the local scripts and then refer a function from the Module Script?
Could you perhaps provide a simple code example if possible?
Thanks!!

If you don’t want to mess with the entire current structure of your game (ie if you want to keep all the scripts separate), chose one of your LocalScrips for a unique UserInputService.

Here is a sample in LocalScript where you can control a key combination (in this case if key Ctrl is pressed or not):

local UserInputService = game:GetService("UserInputService")
local CtrlActive = false

UserInputService.InputBegan:Connect(function(Input,GPE)
	local CurrentKey = Input.KeyCode
	if CurrentKey == Enum.KeyCode.LeftControl or CurrentKey == Enum.KeyCode.RightControl then 
		CtrlActive = true
	else
		print((CtrlActive and 'Ctrl+' or '') .. CurrentKey.Name)
	end
end) 

UserInputService.InputEnded:Connect(function(Input)
	local CurrentKey = Input.KeyCode
	if CurrentKey == Enum.KeyCode.LeftControl or CurrentKey == Enum.KeyCode.RightControl then CtrlActive = false end
end)

You’ll get something like:

D
Ctrl+D
Ctrl+G
J
Ctrl+J


TLDR:
ModuleScript is like any other script, but with the advantage that the functions within it can be shared with other scripts.

But, at this time, forget about ModuleScript, because from what you said, you’re controlling a key in one script and another key in another, and this doesn’t work well for key combinations.
In this case, I suggested unifying everything in a single LocalScript, because as I said before, you don’t need to have 1 script for each object.

1 Like

Thats why you should do

if UserInputService:IsKeyDown(Enum.Keycode.W) then

end

And do it for every button you want

2 Likes

This one I didn’t know about and it actually saves a few lines of code. Thanks.

1 Like