Substitute for playermodule:GetControls()?

Hello! I am working on a game with custom movement, and I need access to the user’s input direction, whether it be from keyboard or controller. I have been using the playerModule’s GetControls() method to get input for movement, but I’ve found that trying to access the playerModule is unreliable and doesnt work 50% of the time (it seems like the game inserts playerModule, deletes it and replaces it?? because of this, :WaitForChild() doesnt work very well), stopping any code that is reliant on it. Before I go and make a module for this myself, is there any commonly used ways to fix this issue?

3 Likes
--game loading
if game:IsLoaded() == false then
	repeat task.wait() until game:IsLoaded()
end
print("loaded")

--core references 
local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")
local UIS = game:GetService("UserInputService")
local inputModule = require(script.Parent.Inputs)
local plr = Players.LocalPlayer
local Camera = workspace.Camera
local Controller = game.StarterPlayer.StarterPlayerScripts:FindFirstChild("PlayerModule") or game.StarterPlayer.StarterPlayerScripts:WaitForChild("PlayerModule", 2)
Controller = require(Controller):GetControls()
print(Controller)

Interestingly, the first print statement executes while the second one doesnt. The only yielding piece of code is the WaitForChild(“PlayerModule”, 2) which has a 2 in the timeout parameter and should pass after 2 seconds have passed. But nothing happens, the print doesnt print and there are no errors either.

3 Likes

just use math.huge instead of waiting 2s because you know it will be there (the playermodule doesn’t delete itself)

--core references 
local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")
local UIS = game:GetService("UserInputService")
local StarterPlayer = game:GetService("StarterPlayer")
local inputModule = require(script.Parent.Inputs)
local plr = Players.LocalPlayer
local Camera = workspace.Camera
local Controller = StarterPlayer.StarterPlayerScripts:WaitForChild("PlayerModule", math.huge)
Controller = require(Controller):GetControls()
print(Controller)

Do you have a custom character loading system? You mentioned you have custom characters, so maybe you’re not waiting for the character to load

entering nil for the second parameter is the same as math.huge

That said, I did end up managing to solve the issue, it wasnt actually related to that line at all. Thank you for replying and offering to help though, I appreciate it!

I believe the script in StarterPlayer.StarterPlayerScripts is just a template that is copied over to each player’s PlayerScripts. To get the correct module I believe you need to get each player’s Player.PlayerScripts:WaitForChild(“PlayerModule”).

1 Like

Hey! Having the exact same issue you had right now could you say what the problem was and how you fixed it.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.