Argument 2 missing or nil

i cant get this script to work without having the onclientevent at the start how can i make it so it will work with the mobile button input?? The local mobilebutton will get an argument 2 missing or nil error

GuiEvent.OnClientEvent:Connect(function(Tuple, Slot)
function onButtonPress()
if Tuple == "SendingSlot" then
inputservice.InputBegan:connect(function(i,g)
GuiEvent:FireServer("StartHack", Slot)
controls:Disable()
end)
end
end
end)

local mobilebutton = contextActionService:BindAction("SprintButton",onButtonPress,true,Enum.KeyCode.E)

Edit: I just realized that you have the function inside of the OnClientEvent event so you can’t do that because it is out of the scope. Try taking the function out of the OnClientEvent. That’s why it’s saying it’s nil.

The issue is the event wasn’t fired before BindAction was called therefore the global onButtonPress variable couldn’t be declared. You should make it local, and put it at the top of the script, making necessary changes so it works, and

@AdvancedDrone this is not correct, roblox implicitly passes the player thru FireServer you don’t do this yourself or this could be a security issue as exploiters could firing on behalf of another player

You have to bind the action from inside OnClientEvent so that the function is also bound at the time it was created:

GuiEvent.OnClientEvent:Connect(function(Tuple, Slot)
	local function onButtonPress()
		if Tuple == "SendingSlot" then
			inputservice.InputBegan:connect(function(i, g)
				GuiEvent:FireServer("StartHack", Slot)
				controls:Disable()
			end)
		end
	end
	contextActionService:BindAction("SprintButton", onButtonPress, true, Enum.KeyCode.E)
	local mobilebutton = contextActionService:GetButton("SprintButton")
	-- do stuff with mobilebutton
end)

This will unbind the previous action that was set to the "SprintButton" name at the time and bind a new one in place. If you don’t want this behavior, you can simply change the name of the action, and CAS will just bind over it instead, and the previous action will be active the moment the current action is unbound.

3 Likes