--README FOR EVERYONE:
--
-- Congratulations on your decision to open this script. If you're not already experienced with scripting, this is the first step on a journey that will improve you as
-- a developer, a future employee, and as a person. Don't let anything discourage you!
--
-- If you would like to tweak this gun, you should look at the ModuleScript named ModelConfiguration that is also a child of the Model in this Tool object. It is where
-- this specific weapon type's configuration values live.
--
--README FOR ADVANCED USERS:
--
-- This tool is part of a common "WeaponsSystem" set. There should be shared code found under the ServerWeaponsScript in a folder called WeaponsSystem if you got it from the Toolbox.
-- If you want to trim down the size of your place file, you can move this WeaponsSystem folder to be a descendant of ReplicatedStorage. It doesn't matter where, as long
-- as WeaponsSystem:IsDescendantOf(ReplicatedStorage) passes.
--
-- IMPORTANT: In order for the tools to find the WeaponsSystem folder if you have copied it yourself, the folder MUST be tagged as "WeaponsSystemFolder"! This system
-- absolutely relies on that fact. If you aren't familiar with the Tag Editor plugin, there is still an easy way to do this.
--
-- 1. Ensure the Command Bar is visible, this can be toggled in the VIEW tab in Studio.
-- 2. Select the WeaponsSystem folder that you want to be used.
-- 3. Copy and paste this code into the command bar: 	game:GetService("CollectionService"):AddTag(game.Selection:Get()[1], "WeaponsSystemFolder")
--
-- It is important to remember that this WeaponsSystem folder, ServerWeaponsScript, and ClientWeaponsScript are all versioned. This is tracked via an IntValue named "Version"
-- that is a direct child of each. These values must all have the same value in order to work. Of course, these values should be set when you download the model. If you decide
-- to change them manually, there are no guarantees that everything will work. It is not recommended to try to change these yourself.
--
-- Fear not, however, because you can actually still have multiple versions of the WeaponSystem in the same game. You can even rename the folders to differentiate them at a glance
-- if you'd like, as long as they still have the correct tag and the correct Version value.
--
-- If you want to create your own weapon types, please look at the WeaponTypes folder under the WeaponsSystem folder.



local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CollectionService = game:GetService("CollectionService")
local RunService = game:GetService("RunService")
local ServerScriptService = game:GetService("ServerScriptService")

local WEAPONS_SYSTEM_TAG = "WeaponsSystemFolder"

local versionObj = script:FindFirstChild("Version")
if not versionObj then
	error("ServerWeaponsScript does not contain an IntValue called Version. Cannot determine what WeaponsSystem to use.")
	return
end
local ScriptVersion = versionObj.Value

RunService.Heartbeat:Wait()
local ServerWeaponsScript = nil
for i, child in pairs(ServerScriptService:GetChildren()) do
	if child:IsA("Script") and child.Name == script.Name and CollectionService:HasTag(child, "ServerWeaponsSystem") then
		local childVersion = child:FindFirstChild("Version")
		if childVersion and childVersion:IsA("IntValue") and childVersion.Value == ScriptVersion then
			ServerWeaponsScript = child
			break
		end
	end
end

if ServerWeaponsScript and ServerWeaponsScript ~= script then
	local localWeaponsSystem = script:FindFirstChild("WeaponsSystem")
	if localWeaponsSystem then
		localWeaponsSystem:Destroy()
	end
	return
else
	CollectionService:AddTag(script, "ServerWeaponsSystem")
	script.Parent = ServerScriptService
end
print("Starting ServerWeaponsScript")

local WeaponsSystemFolder = nil
local function searchForWeaponsSystem()
	for i, weaponsSystem in pairs(CollectionService:GetTagged(WEAPONS_SYSTEM_TAG)) do
		if weaponsSystem:IsDescendantOf(ReplicatedStorage) then
			local systemVersionObj = weaponsSystem:FindFirstChild("Version")
			if systemVersionObj and systemVersionObj.Value == ScriptVersion then
				WeaponsSystemFolder = weaponsSystem
				break
			end
		end
	end
end

searchForWeaponsSystem()
if not WeaponsSystemFolder then
	local weaponsSystemTemplate = script:FindFirstChild("WeaponsSystem")
	if weaponsSystemTemplate then
		WeaponsSystemFolder = weaponsSystemTemplate:Clone()
		local librariesFolder = ReplicatedStorage:FindFirstChild("Libraries")
		WeaponsSystemFolder.Parent = librariesFolder or ReplicatedStorage

		CollectionService:AddTag(WeaponsSystemFolder, WEAPONS_SYSTEM_TAG)
	else
		--Guess we just need to wait for someone else to instantiate the proper WeaponsSystem.
		warn(string.format("The instance \"%s\" did not contain a WeaponsSystem folder and a matching version (%d) of the WeaponsSystem folder could not be found as a descendant of ReplicatedStorage. Did you forget to install the WeaponsSystem folder to ReplicatedStorage?",
			script.Parent:GetFullName(),
			ScriptVersion
		))
		while not WeaponsSystemFolder do
			CollectionService:GetInstanceAddedSignal(WEAPONS_SYSTEM_TAG):Wait()
			searchForWeaponsSystem()
		end
	end
end

assert(WeaponsSystemFolder, "No WeaponsSystem found, search failed in an interesting way?")

local WeaponsSystemModule = WeaponsSystemFolder:FindFirstChild("WeaponsSystem")
if not WeaponsSystemModule or not WeaponsSystemModule:IsA("ModuleScript") then
	error("No WeaponsSystemModule in the WeaponsSystem found!")
end

local WeaponsSystem = require(WeaponsSystemModule)
if not WeaponsSystem.doingSetup and not WeaponsSystem.didSetup then
	WeaponsSystem.setup()
end