All weapons in game controlled by 1 script

So I am trying to use one script to control all my weapons in the game. Right now when I swap between weapons and activate them it starts to double the code that is running.

Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		char.ChildAdded:Connect(function(child)

			if child:IsA("Tool") and child.Name == "Blue Sword" then
				local wepType = child:GetAttribute("Type")
				local class = child:GetAttribute("Class")
				local dmg = child:GetAttribute("Damage")
				local cd = child:GetAttribute("Cooldown")

				child.Activated:Connect(function()
					print(child.Name.." the "..class.." was activated!")
				end)
			elseif child:IsA("Tool") and child.Name == "Red Machete" then
				local wepType = child:GetAttribute("Type")
				local class = child:GetAttribute("Class")
				local dmg = child:GetAttribute("Damage")
				local cd = child:GetAttribute("Cooldown")

				child.Activated:Connect(function()
					print(child.Name.." the "..class.." was activated!")
				end)
			end

		end)
	end)
end)

3 Likes

It does this since its a new child being added to the character EVERY TIME. You could do a WaitForChild and do it only once.

1 Like

Do you have any examples I can look at for reference?

1 Like

None on hand no, I’d just do it from a script inside. But thats me.

I am tired of reformatting every weapon’s code when I update one of the scripts to be better.

This is pain to do edit every time for every weapon, changing animations, etc

--//Services\\--
local repStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

--//Assets\\--
local assets = repStorage.Preload.Assets

local sounds = assets.Sounds
local anims = assets.Anims
local remotes = assets.Remotes

--//Modules\\--
local modules = assets.Modules
local sLib = require(modules.ServerLibrary)
local infectModule = require(modules.InfectHandler)
local eff = require(modules.Effects)

--//Weapon Stats\\--
local item = script.Parent
local dmg = item:GetAttribute("Damage")
local cd = item:GetAttribute("Cooldown")

--//Checks\\--
local active = false
local debounce = false
local dmgCD = false

function ActiveCheck()
	if not debounce then
		debounce = true
		active = true --this verifies that tool is activated

		task.wait(cd)

		active = false --this verifies that tool is no longer active
		debounce = false
	end
end

function equip()
	local char = item.Parent
	sLib.play_sound(sounds.macheteEquip,char.HumanoidRootPart)
end

local function hitSound()
	local pitch = nil

end

function Swing()
	item.Handle.Blade.Touched:Connect(function(hit)
		if hit.Parent and hit.Parent:FindFirstChild("Humanoid") and hit.Parent ~= item.Parent then
			local victim = hit.Parent --the character of the player getting attacked
			local char = item.Parent--the character of the player attacking

			local vplr = game.Players:GetPlayerFromCharacter(victim) --the player getting attacked
			local plr = game.Players:GetPlayerFromCharacter(char) --the player attacking
			
			sLib.play_sound(sounds.macheteMiss,char.HumanoidRootPart)
			
			local pitch = nil
			
			if vplr then

				if vplr.Status:GetAttribute("infClass") == "N/A" then
					if not dmgCD then
						if active == true then
							dmgCD = true
							sLib.Dmg(victim,dmg,0.2)
							
							if math.random(1,3) == 1 then
								pitch = 1
							elseif math.random(1,3) == 2 then
								pitch = 0.9
							elseif math.random(1,3) == 3 then
								pitch = 0.8
							end
							
							sLib.play_sound(sounds.macheteHit,victim.HumanoidRootPart,pitch)

							task.wait(cd)
							dmgCD = false
						end
					end
				end

			else

				if not dmgCD then
					if active == true then
						dmgCD = true
						sLib.Dmg(victim,dmg,0.2)
						
						if math.random(1,3) == 1 then
							pitch = 1
						elseif math.random(1,3) == 2 then
							pitch = 0.9
						elseif math.random(1,3) == 3 then
							pitch = 0.8
						end

						sLib.play_sound(sounds.macheteHit,victim.HumanoidRootPart,pitch)

						task.wait(cd)
						dmgCD = false
					end
				end

			end
			
		end
	end)
end

local function swingAnim(char)

	if not debounce then
		debounce = true
		active = true --this verifies that tool is activated

		sLib.playAnim(anims['macheteSwing'],item.Parent.Humanoid)
		task.wait(cd)

		active = false --this verifies that tool is no longer active
		debounce = false
	end

end

function Damage()
	swingAnim()
	Swing()
end

item.Equipped:Connect(ActiveCheck)
item.Equipped:Connect(equip)

item.Activated:Connect(ActiveCheck)
item.Activated:Connect(Damage)

Well thats what a rather complex tool looks like on the inside. Thats not something we can do

I want to try using collection service as well as attributes, but I haven’t had any luck yet
Screenshot 2024-01-15 154443

Any idea on what I could try?

No, no advice from here on. I don’t make tools. I just script.

CollectionService | Documentation - Roblox Creator Hub

Doesn’t print at all now

for i, weapon in pairs(collect:GetTagged("Weapon")) do
	
	if weapon:IsA("Tool") then
		if weapon.Parent:FindFirstChild("Humanoid") then
			local char = weapon.Parent
			
			if weapon.Name == "Blue Sword" then
				weapon.Activated:Connect(function()
					print("e")
				end)
				elseif weapon.Name == "Red Machete" then
				weapon.Activated:Connect(function()
					print("e2")
				end)
			end
		end
	end
end

maybe instead of hard-coding names in there you could do something a little bit different?
(with this approach, try printing the associated stats. this is where the beauty of collection service can really shine.)

local collectionService = game:GetService("CollectionService")
local playerService = game:GetService("Players")

local function addItemToBackpack(plr)
	-- there are no issues after the player dies
	plr.CharacterAdded:Connect(function()
		task.wait(5) -- waiting for the game to initially load, you might want to do something else instead
		for _, weapon in ipairs(collectionService:GetTagged("Weapon")) do
			task.spawn(function()
				local weaponClone = weapon:Clone()
				weaponClone.Parent = plr.Backpack
				print(weapon.Name .. " added to backpack")

				weaponClone.Activated:Connect(function()
					print(weaponClone.Name .. " was activated")
				end)
			end)
		end
	end)
end

playerService.PlayerAdded:Connect(addItemToBackpack)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.