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.
local Gun = {}
function Gun.PlayAnimation(self, name)
if name == "Reload" then
print("reloading..")
end
end
function Gun:Reload()
self:PlayAnimation("Reload")
end
Gun:Reload()
^^ 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.