--local Player=ModFx.GetPlayerFromTool(Tool) --[[This will be refreshed throughout the script 
--											so that if someone wanted to make the tools 
--											droppable, itd work with other players	]]
-- commented cause its useless as a global variable, however not deleted so as to keep the above comments
local ColSer=game:GetService("CollectionService")
local Debris=game:GetService("Debris")

local function FireWep(plr,targetPos)
	if Tool.Enabled==false then return end
	local Player=ModFx.GetPlayerFromTool(Tool)
	if plr~=Player then return end
	local c=plr.Character
	if not c then return end
	local h=c:FindFirstChildWhichIsA("Humanoid")
	local plrTool=c:FindFirstChildWhichIsA("Tool")
	local Head=c:FindFirstChild("Head")
	if not Head or not plrTool or not h then return end
	if plrTool~=Tool then return end
	if h.Health<=0 then return end
	
	Tool.Enabled=false
	
	local dir = targetPos - Head.Position
	dir = dir*(1/dir.magnitude)
	
	local pos = Head.Position + (dir * 8)
	local ad=5000

	local missile = Rocket:clone()
	missile.Name="Rocket"
	
	local BV=Instance.new("BodyVelocity")
	BV.Parent=missile
	BV.P=12500
	BV.MaxForce=Vector3.new(ad,ad,ad)
	BV.Velocity=dir*60
	missile.CFrame = CFrame.new(pos,  pos + dir)
	
	local creator_tag = Instance.new("ObjectValue")
	creator_tag.Value = plr
	creator_tag.Name = "creator"
	creator_tag.Parent = missile
	
	local exp=Tool.Handle.exp:Clone()
	exp.PlaybackSpeed=math.random(90,110)/100
	exp.PlayOnRemove=true
	exp.Parent=missile
	
	local swoosh=Tool.Handle.swoosh:Clone()
	swoosh.PlaybackSpeed=math.random(95,105)/100
	swoosh.Parent=missile

	missile.Parent = workspace
	ColSer:AddTag(missile,missile.Name) 
	ColSer:AddTag(missile,plr.Name)
	missile:SetNetworkOwner(plr)
	swoosh:Play()
	
	local effect=plr:FindFirstChild("Effect")
	if effect and effect.Value~="None" then
		if effect.Value=="Fire" then
			Instance.new("Fire",missile)
		else
			spawn(function()
				require(game:GetService("ReplicatedStorage").TrailModule).AddTrail(missile,effect.Value,.5,.5,1)
			end)
		end
	end
	Debris:AddItem(missile,10)
	wait(7)
	Tool.Enabled=true
end

Tool.Fire.OnServerEvent:Connect(FireWep)

local function DestroyRocket(plr,Obj,ClientPos)
	local x=true
	local succ,err=pcall(function() 
		if Obj:GetNetworkOwner()~=plr then x=false end 
	end) 
	if err then warn(plr.Name.." may be exploiting") return end -- Not sure how this would happen.
	if x==false then warn(plr.Name.." is definitely exploiting") --[[plr:Kick("Possible exploit detected")]] return end
	--[[ Why am I so confident that they're an exploiter if x==false? Because the network owner of the rocket is SET (as opposed to using auto 
	network ownership) to the rocket's firer, and so if the part this remote is firing upon is NOT owned by the user firing the remote, then I 
	know the remote is being fired externally. ]]
	local ServerPos=Obj.Position
	local creator=Obj:FindFirstChild("creator")
	local CreatorValue
	if creator then
		CreatorValue=creator.Value
	end
	local Player=ModFx.GetPlayerFromTool(Tool)
	if CreatorValue~=nil and CreatorValue==plr and Player and Player==plr and (ServerPos - ClientPos).magnitude < 20 and ColSer:HasTag(Obj,Obj.Name) 
	and ColSer:HasTag(Obj,plr.Name) then -- all these to ensure that it's a rocket, and its their rocket, and its from their tool
		-- "(ServerPos - ClientPos).magnitude" = the distance between the serverside and clientside missile on hit
		Obj:ClearAllChildren()
		Debris:AddItem(Obj,0)
		ModFx.Explosion(CreatorValue,"Rocket",ClientPos)
	end
end

Tool.RocketDestroyEvent.OnServerEvent:connect(DestroyRocket)