Function firing when the game starts?

So I have this code which appears to run when the game starts - I didn’t think functions did that and now I’m confused. Here is the output.
I’m at a loss to explain this. Can anyone help me out?
blob.png

Tabs = {"Boosts","Trails","Coins"}
currently = 1

function changeTab(actionName, state, inputObject)
	print("Fired1")
	if state == Enum.UserInputState.Change or state == Enum.UserInputState.End then return end
	print(inputObject.KeyCode)
	if inputObject.KeyCode == Enum.KeyCode.ButtonL1 then
		print("Hi")
	
	--script.Parent:FindFirstChild(Tabs[currently]).Visible = true
    end
end

game:GetService("ContextActionService"):BindAction("changeTab",changeTab,false,Enum.KeyCode.ButtonL1)

An KeyCode has two ‘properties’. Name (string) and Value (int)

Correct code

Tabs = {"Boosts","Trails","Coins"}
currently = 1

function changeTab(actionName, state, inputObject)
	print("[Key] The key Fired1 has been pressed.")
	if state == Enum.UserInputState.Change or state == Enum.UserInputState.End then return end
	if inputObject == Enum.KeyCode.ButtonL1 then
		print("Hi")
	
	--script.Parent:FindFirstChild(Tabs[currently]).Visible = true
        end
end

game:GetService("ContextActionService"):BindAction("changeTab",changeTab,false,Enum.KeyCode.ButtonL1)

Uh, inputObject is actually an InputObject, not a KeyCode. The code OP posted is correct.

Sorry my fault :wink:

1 Like

I’ve had a lot of problems with ContextActionService, maybe I should use UserInputService instead? Binding actions and unbinding them just so you can press Y to change the shown GUI seems a bit odd.

Nah, ContextActionService is probably the right thing to do for input. Try printing the type (InputObject.UserInputType) - chances are you’re probably getting something different.

My problems with CAS go further than this. Its acting really strangely all the time. I’ve keybound ButtonY to mean “advance” and now it automatically advances for me. Its such a mess :confused:

I get “Enum.UserInputType.None” when I print out the UserInputType. gg roblox (although its probably my fault)

Also try printing the UserInputState, because you may be getting Enum.UserInputState.Cancel. Either way, you can add UserInputType.None and UserInputState.None to your if statement to make sure that you’re not getting any ‘invalid’ input.

Will do. I ragequitted and got rid of all my changes. Starting over again to take it step by step, keybind by keybind.

1 Like

I’ve just realised the best way to do this is with an auxillary function which checks if its a valid gamepad input (as I also use MouseButton1Click for PC players) and it looks like it is working as intended so far, albeit I haven’t rewritten the code from this question yet.

Hmmm… I’ve managed to code everything in except for my spectate ui. When the player wants to view the next player they can either press R1 or L1 to cycle. However, my code just isn’t working.
The changeCamXbox method just isn’t firing, yet the Spectating.changed function already worked and I’ve just added the Bind and Unbind commands, yet pressing R1 or L1 does nothing. Anyone have any ideas? @digpoe

function changeCamXbox(actionName, actionInputState, actionInputObject)
	print(actionName)
	if actionInputState == Enum.UserInputState.Begin then
		if actionName == "Next" then
			increment()
		elseif actionName == "Back" then
			decrement()
		end
	end
end

script.Parent.Spectating.Changed:connect(function(value) --player starts spectating
	if value == false then
		local cam = workspace.CurrentCamera
		cam.CameraSubject=game.Players.LocalPlayer.Character.Humanoid
		script.Parent.PlrName.Value = ""
		script.Parent.Visible = false
		game:GetService("ContextActionService"):UnbindAction("Next")
		game:GetService("ContextActionService"):UnbindAction("Back")
		
	else
		changeCamera()
		script.Parent.Visible = true
		game:GetService("ContextActionService"):BindAction("Next",changeCamXbox,false,Enum.KeyCode.ButtonR1)
		game:GetService("ContextActionService"):BindAction("Back",changeCamXbox,false,Enum.KeyCode.ButtonL1)
	end
end)