Self not working in function

I realize I am doing this wrong I would just like a response on what the proper way to do this would be greatly appreciated.

ContextActionService:BindAction("Reload", self.Reload, false, Enum.KeyCode.R)

function Gun:Reload()
    self:PlayAnimation("Reload")
end

The error is appearing when I press R so it is not an issue with context action service however with how you get self.

I think its this

function Gun:Reload()
    self:PlayAnimation("Reload")
end

ContextActionService:BindAction("Reload", Gun:Reload, false, Enum.KeyCode.R)

Also, if you want to make a reload script try this script (found it on the DevHub):

local ContextActionService = game:GetService("ContextActionService")
 
local ACTION_RELOAD = "Reload"
 
local tool = script.Parent
 
local function handleAction(actionName, inputState, inputObject)
	if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
		print("Reloading!")
	end
end
 
tool.Equipped:Connect(function ()
	ContextActionService:BindAction(ACTION_RELOAD, handleAction, true, Enum.KeyCode.R)
end)
 
tool.Unequipped:Connect(function ()
	ContextActionService:UnbindAction(ACTION_RELOAD)
end)

I am sorry I should’ve been more clear the issue isn’t with context action service as it is working perfectly fine as whenever I press R the error appears but instead with how you get self as it is marking self as nil and I am unaware of a scenario on where you get self and bring it through the function.

1 Like

This is still available if anyone wants to attempt to help me please any help would be greatly appreciated!

I might be wrong but I think it might need to be Gun.Reload instead of Gun:Reload. I have no idea if I’m right or not.

Yes I could in fact do that however the problem with that is that it makes it so self is now nill

This code works:

local Gun = {}

function Gun.PlayAnimation(self, name)
	if name == "Reload" then
		print("reloading..")
	end
end

function Gun:Reload()
	self:PlayAnimation("Reload")
end

Gun:Reload()

Output:
image

^^ Gun.Reload is not defined in the first line here. Maybe it’s trying to do nil() and nil is not a function so it says “attempt to call a nil value” Try doing like print(self) print(self.Reload) before the ContextActionService error line.