Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Gamepad Detection System not working properly - can someone help?

Hello, everyone.

I have a game that will be played by both people on Xbox One and people on PC.

The only problem is that while it works if you connect or disconnect a controller while the game is running, and the buttons appear or disappear, respectfully, I can't seem to make it detected an absent gamepad or one that's plugged in currently, making the buttons either disappear or reappear respectfully.

I'd appreciate some help with this. Any help is much appreciated.






while true do 
wait(0)
if game:GetService("UserInputService").GamepadEnabled then
	
	game:GetService("UserInputService").GamepadConnected:connect(function(gamepad)
	script.Parent.Visible = true --Make the gamepad buttons visible
	game:GetService("UserInputService").GamepadDisconnected:connect(function(gamepad)
	script.Parent.Visible = false --Make the gamepad buttons invisible


end)
	end)
end
end



1 answer

✓ Accepted answer

This should work for you, much more efficient than a loop too!

I've encountered trouble trying to detect a gamepad before it can be detected, so the character + 2 second wait are just there because I'm paranoid.

repeat wait() until game.Players.LocalPlayer.Character
wait(2)
if game:GetService("UserInputService").GamepadEnabled then
	script.Parent.Visible = true
end

game:GetService("UserInputService").GamepadConnected:connect(function(gamepad)
	script.Parent.Visible = true
end)
game:GetService("UserInputService").GamepadDisconnected:connect(function(gamepad)
	script.Parent.Visible = false
end)

1 comment

0

Thank you! This works perfectly. c:

This discussion is preserved as it was. New answers, comments, and votes are closed.