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

Why is this simple gamepad connect localscript not working?

I have a red part in the Workspace and a LocalScript in the StarterGui.

The LocalScript is supposed to turn the color of the Part to Bright green if Gamepad1 is connected.

I've tested this with my USB Xbox controller to no avail. Absolutely nothing happens -- the print functions don't even appear in the output.

Here's the LocalScript:

local part = game.Workspace.Part
print('this doesnt appear in the output')
if game:GetService("UserInputService").GamepadEnabled then
	print('Gamepad Enabled')
	if game:GetService("UserInputService"):GetGamepadConnected(Enum.UserInputType.Gamepad1) then
		print('Gamepad1 Connected')	
		part.BrickColor=BrickColor.new('Bright green')
	end
end	

Here's a screenshot of my explorer: http://i.imgur.com/EFAwOsb.png Here's a screenshot of my output: http://i.imgur.com/ppsB1US.png Here's a screenshot of the actual place: http://i.imgur.com/SOk46sF.png

If you need anymore information, just let me know. Many thanks if you are able to help!

EDIT: Turns out that when I play solo instead of hitting "Play", the print function that says, "this doesnt appear in the output" appears in the output. Everything else doesn't work, though. So here's an updated picture of the output: http://i.imgur.com/ZD4JQGd.png

1 answer

✓ Accepted answer

All you need to do is wait for the character to load otherwise the script will not work:-

local part = game.Workspace.Part
wait(3) --You need to wait for the character. There are better ways of doing this but you should be using the events
print('this doesnt appear in the output')
if game:GetService("UserInputService").GamepadEnabled then
    print('Gamepad Enabled')
    if game:GetService("UserInputService"):GetGamepadConnected(Enum.UserInputType.Gamepad1) then
        print('Gamepad1 Connected') 
        part.BrickColor=BrickColor.new('Bright green')
    end
end 

You should be using the events:-

game:GetService("UserInputService").GamepadConnected:connect(function(gamepad)
	print("Player has plugged in controller: " .. tostring(gamepad))
end)
 
game:GetService("UserInputService").GamepadDisconnected:connect(function(gamepad)
	print("Player has unplugged in controller: " .. tostring(gamepad))
end)

Just some more info Controller Support

1 comment

0

Thanks a bunch! :)

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