I’m working on a weapon reload system where the player can run into ammo that’s placed on the map and it reloads the corresponding weapon. The problem that I’m running into is that there doesn’t seem to be a way to call a function in the tool script from the module script that’s handling the reload. I have tried several things and none of them seem to work. I get an error saying that the function is not a valid member of the script, and yet it is.
Here are the relevant functions in the tool script:
-- Allows an external caller to add ammo to the weapon.
function ReloadAmmo(player, amount)
toolData.weapon.ammo += amount
if toolData.weapon.ammo > toolData.weapon.ammoMax then
toolData.weapon.ammo = toolData.weapon.ammoMax
end
script.Parent.Click:FireClient(player, 0)
end
-- Returns the weapon data to the caller.
function GetToolData()
local data = recursiveCopy(toolData)
return data
end
And here is the code from the module script that is giving the error:
local toolList = player.Backpack:GetChildren()
for _, tool in pairs(toolList) do
local toolData = tool.ServerScript.GetToolData()
if toolData.toolType == enums.ToolType.Weapon then
if ammoType == toolData.weapon.projectile.projType then
tool.ServerScript.ReloadAmmo(player, ammoAmount)
break;
end
end
end
It stops on an error when I try to call GetToolData() which says that GetToolData is not a valid member of the tool script…yet it’s in there, and it does not have the local qualifier. The tool script is an actual script, not a module or local script. I do have a workaround where the tool registers itself with another module script that I can call, but that is far from ideal.