Ok, so I updated this post because my code works, just needs simplification. First I want to be able to shoot while not having a target and I want this code to be more readable while keeping camelCase.
So here’s the code:
LOCAL:
-- Vars
local players = game:GetService('Players')
local player = players.LocalPlayer
local mouse = player:GetMouse()
local rs = game:GetService("ReplicatedStorage")
local class = require(rs.class)
local tool = player.Character and player.Character:FindFirstChildWhichIsA("Tool")
local guns = { 'gun', 'deagle'}
local swords = { 'sword' }
local contextActionService = game:GetService("ContextActionService")
local targetHum
-- Functions
local function onToolEquipped(tool)
tool = tool
print("Equipped tool:", tool.Name)
end
local function onToolUnequipped(tool)
tool = nil
print("Unequipped tool")
end
contextActionService.LocalToolEquipped:Connect(onToolEquipped)
contextActionService.LocalToolUnequipped:Connect(onToolUnequipped)
local function checkTool(tool)
if table.find(guns, tool.Name) then
local coolDown = false
tool.Activated:Connect(function()
if not coolDown then
local target = mouse.Target
if target.Name == "Handle" then
targetHum = target.Parent.Parent:FindFirstChild("Humanoid")
else
targetHum = target.Parent:FindFirstChild("Humanoid")
end
print(targetHum)
local humanoid = targetHum
class.gunClass.attack(tool, humanoid, target)
coolDown = true
print("cool")
task.wait(tool:GetAttribute("coolDown"))
coolDown = false
print("ready")
end
end)
elseif table.find(swords, tool.Name) then
local coolDown = false
tool.Activated:Connect(function()
if not coolDown then
local humanoid = mouse.Target.Parent:FindFirstChild("Humanoid")
class.swordClass.attack(tool, humanoid)
coolDown = true
task.wait(tool:GetAttribute("coolDown"))
coolDown = false
end
end)
end
end
player:WaitForChild("Backpack").ChildAdded:Connect(function(tool)
if tool:IsA('Tool') then
checkTool(tool)
end
end)
player.CharacterAdded:Connect(function()
for item, tool in ipairs(player:WaitForChild("Backpack"):GetChildren()) do
print(tool)
if tool:IsA('Tool') then
checkTool(tool)
end
end
end)
MODULE:
-- Mod
local class = {}
class.name = "class"
--Vars
local players = game:GetService("Players")
local player = players.LocalPlayer
local RS = game:GetService("ReplicatedStorage")
-- Classes
class.gunClass = {}
class.swordClass = {}
-- Functions
function class.gunClass.attack(tool, humanoid, target)
print("clicked")
RS:WaitForChild("shot"):FireServer(tool, humanoid, target)
end
function class.swordClass.attack(tool, humanoid)
print("clicked")
RS:WaitForChild("swung"):FireServer(tool, humanoid)
end
-- Return
return class
SERVER:
local RS = game:GetService("ReplicatedStorage")
-- Create a table to store the damaged targets
local damaged = {}
-- swung
RS:WaitForChild("swung").OnServerEvent:Connect(function(player, tool, humanoid)
print("swung")
print(tool)
end)
-- shot
RS:WaitForChild("shot").OnServerEvent:Connect(function(player, tool, humanoid, target)
print("shot")
-- Check if the target has already been damaged
if not damaged[target] then
-- Mark the target as damaged
damaged[target] = true
if humanoid and target.Name == "Head" then
humanoid:TakeDamage(tool:GetAttribute("headShot"))
print(humanoid.Health)
table.clear(damaged)
elseif humanoid then
humanoid:TakeDamage(tool:GetAttribute("damage"))
print(humanoid.Health)
table.clear(damaged)
end
else
-- Target has already been damaged, ignore this shot
print("Target has already been damaged.")
end
-- Clean up the damaged table
table.clear(damaged)
end)