Melee Weapon damage?

Hi people, my second topic created.

For context, i’m a noob at scripting, right now i’m trying to actually script something and not use help unless I absolutely need it, and well? i’m here!

Here i’m trying to make a bat system, where if player hold down their mouse they’ll slowly charge, and if overcharged the animation stop and charge is drop back to 0, the higher the number the more the damage, when the mouse is released, I want it to check the charge power, play animation and audio, then maybe send damage value to server script and make it deal damage to player that has been striked.

Problem? I don’t know how to properly do that. Right now there are nothing that I wrote in server script, here’s the layout of the bat in workspace.
image
(Ignore Idle and Equipped, they’re working fine and i’m 100% sure it have nothing to do with this)
The animation will be deleted later, I’m just placing them there so I can grab ID.

This is what its looks like in localscript “Sword” (I’m terrible right now, so things may be very unorganized)

local Player = game:GetService("Players")
local player = Player.LocalPlayer

repeat wait(1) until player.Character

local character = player.Character
local humanoid = character:WaitForChild("Humanoid")

--                  Animation
--Charge Animation
local animation = Instance.new("Animation")
animation.Name = "Charge"
animation.Parent = script.Parent

animation.AnimationId = "rbxassetid://6245638937"

--Light Hit
local LightHitAnim = Instance.new("Animation")
LightHitAnim.Name = "LightSwing"
LightHitAnim.Parent = script.Parent

animation.AnimationId = "rbxassetid://6245660168"

--Hard Hit
local HardHitAnim = Instance.new("Animation")
HardHitAnim.Name = "HardSwing"
HardHitAnim.Parent = script.Parent

animation.AnimationId = "rbxassetid://6245655370"

--Load Animation to play later
local animtrack = humanoid:LoadAnimation(animation)
local LightHitTrack = humanoid:LoadAnimation(LightHitAnim)
local HardHitTrack = humanoid:LoadAnimation(HardHitAnim)

local Player = game:GetService("Players")
local Mouse = Player.LocalPlayer:GetMouse()
local ChargePower = 0

local HoldingMouse = false

function TagHumanoid(hit)
	--I'm stuck here
end

--Holding mouse
Mouse.Button1Down:Connect(function()
	mouseDown = true
	animtrack:Play()
	wait(0.5)
	if mouseDown == true then
		local ChargePower = 1
		print(ChargePower)
		wait(1)
		if mouseDown == true then
			local ChargePower = 2
			print(ChargePower)
			wait(1)
			if mouseDown == true then
				local ChargePower = 3
				print(ChargePower)
				wait(0.5)
				if mouseDown == true then
					mouseDown = false
					animtrack:Stop()
					local ChargePower = 0
					print(ChargePower)
				end
			end
		end
	end
end)

--Mouse released
Mouse.Button1Up:Connect(function()
	mouseDown = false
	animtrack:Stop()
	if ChargePower == 1 then
		LightHitTrack:Play()
	end
	
end)

Ok so, recap, my goal is:

  1. Player hold mouse to charge bat
  2. Attack when mouse is released, and check damage value to deal damage
    I’m also trying to make it take away some stamina but that’s not the main problem right now, for now I just need some tips and advice, I’ve heard about humanoid:TakeDamage() but I still don’t know how to use it.
    I’d appreciate your tips and advice on how to improve my script and reach my goal.
    Explain things on the way maybe.
    Edit: Updated my code, the localscript is working fine now but now I need to work on sending data to server and also make it deal damage.

“Sword” Local script:

local Player = game:GetService("Players")
local player = Player.LocalPlayer

repeat wait(1) until player.Character

local character = player.Character
local humanoid = character:WaitForChild("Humanoid")
local CoolDown = false

--                  Animation
--Charge Animation
local animation = Instance.new("Animation")
animation.Name = "Charge"
animation.Parent = script.Parent

animation.AnimationId = "rbxassetid://6245638937"

--Light Hit
local LightHitAnim = Instance.new("Animation")
LightHitAnim.Name = "LightSwing"
LightHitAnim.Parent = script.Parent

LightHitAnim.AnimationId = "rbxassetid://6245660168"

--Hard Hit
local HardHitAnim = Instance.new("Animation")
HardHitAnim.Name = "HardSwing"
HardHitAnim.Parent = script.Parent

HardHitAnim.AnimationId = "rbxassetid://6245655370"

--Load Animation to play later
local animtrack = humanoid:LoadAnimation(animation)
local LightHitTrack = humanoid:LoadAnimation(LightHitAnim)
local HardHitTrack = humanoid:LoadAnimation(HardHitAnim)

local Player = game:GetService("Players")
local Mouse = Player.LocalPlayer:GetMouse()
ChargePower = 0

local HoldingMouse = false

--Holding mouse
Mouse.Button1Down:Connect(function()
	mouseDown = true
	if CoolDown == false then
		animtrack:Play()
		wait(0.5)
	end
	if mouseDown == true and CoolDown == false then
		ChargePower = 1
		print(ChargePower)
		wait(1)
		if mouseDown == true and CoolDown == false then
			ChargePower  = 2
			print(ChargePower)
			wait(1)
			if mouseDown == true and CoolDown == false then
				ChargePower = 3
				script.Parent.Handle.FullyChargedAudio:Play()
				script.Parent.Handle.FullyCharge.Enabled = true
				script.Parent.Handle.FullyCharge2.Enabled = true
				print(ChargePower)
				wait(0.5)
				if mouseDown == true and CoolDown == false then
					mouseDown = false
					animtrack:Stop()
					ChargePower = 0
					script.Parent.Handle.FullyCharge.Enabled = false
					script.Parent.Handle.FullyCharge2.Enabled = false
					print(ChargePower)
				end
end
		end
	end
end)

--Mouse released
Mouse.Button1Up:Connect(function()
	mouseDown = false
	CoolDown = true
	if ChargePower == 0 then
		animtrack:Stop()
		game.ReplicatedStorage.SwordAttack:FireServer(ChargePower)
		wait(3)
		CoolDown = false
	elseif ChargePower == 1 then
		script.Parent.Handle.Swing:Play()
		animtrack:Stop()
		LightHitTrack:Play()
		game.ReplicatedStorage.SwordAttack:FireServer(ChargePower)
		ChargePower = 0
		wait(3)
		CoolDown = false
	elseif ChargePower == 2 then
		script.Parent.Handle.Swing:Play()
		animtrack:Stop()
		LightHitTrack:Play()
		game.ReplicatedStorage.SwordAttack:FireServer(ChargePower)
		ChargePower = 0
		wait(3)
		CoolDown = false
	elseif ChargePower == 3 then
		script.Parent.Handle.Swing:Play()
		animtrack:Stop()
		HardHitTrack:Play()
		script.Parent.Handle.FullyCharge.Enabled = false
		script.Parent.Handle.FullyCharge2.Enabled = false
		game.ReplicatedStorage.SwordAttack:FireServer(ChargePower)
		ChargePower = 0
		wait(3)
		CoolDown = false
	end
end)

Server script

function TargetPlayer(hit)
	humanoid = hit.Parent.Humanoid
end

script.Parent.Handle.Touched:Connect(TargetPlayer)

game.ReplicatedStorage.SwordAttack.OnServerEvent:Connect(function(ChargePower)
	if ChargePower == 0 then
		humanoid:TakeDamage(0)
		if ChargePower == 1 then
			humanoid:TakeDamage(15)
			if ChargePower == 2 then
				humanoid:TakeDamage(35)
				if ChargePower == 3 then
					humanoid:TakeDamage(50)
				end
			end
		end
	end
end)

Here’s the bat so far, no damage yet.

Solved it myself! I actually feel so proud of myself rn.
for those with this problem, here’s what I have in local script:

local Player = game:GetService("Players")
local player = Player.LocalPlayer

repeat wait(1) until player.Character

local character = player.Character
local humanoid = character:WaitForChild("Humanoid")
local CoolDown = false

--                  Animation
--Charge Animation
local animation = Instance.new("Animation")
animation.Name = "Charge"
animation.Parent = script.Parent

animation.AnimationId = "rbxassetid://6245638937"

--Light Hit
local LightHitAnim = Instance.new("Animation")
LightHitAnim.Name = "LightSwing"
LightHitAnim.Parent = script.Parent

LightHitAnim.AnimationId = "rbxassetid://6245660168"

--Hard Hit
local HardHitAnim = Instance.new("Animation")
HardHitAnim.Name = "HardSwing"
HardHitAnim.Parent = script.Parent

HardHitAnim.AnimationId = "rbxassetid://6245655370"

--Load Animation to play later
local animtrack = humanoid:LoadAnimation(animation)
local LightHitTrack = humanoid:LoadAnimation(LightHitAnim)
local HardHitTrack = humanoid:LoadAnimation(HardHitAnim)

local Player = game:GetService("Players")
local Mouse = Player.LocalPlayer:GetMouse()
ChargePower = 0

local HoldingMouse = false

--Holding mouse
Mouse.Button1Down:Connect(function()
	mouseDown = true
	if CoolDown == false then
		animtrack:Play()
		wait(0.5)
	end
	if mouseDown == true and CoolDown == false then
		ChargePower = 1
		print(ChargePower)
		wait(1)
		if mouseDown == true and CoolDown == false then
			ChargePower  = 2
			print(ChargePower)
			wait(1)
			if mouseDown == true and CoolDown == false then
				ChargePower = 3
				script.Parent.Handle.FullyChargedAudio:Play()
				script.Parent.Handle.FullyCharge.Enabled = true
				script.Parent.Handle.FullyCharge2.Enabled = true
				print(ChargePower)
				wait(0.5)
				if mouseDown == true and CoolDown == false then
					mouseDown = false
					animtrack:Stop()
					ChargePower = 0
					script.Parent.Handle.FullyCharge.Enabled = false
					script.Parent.Handle.FullyCharge2.Enabled = false
					print(ChargePower)
				end
end
		end
	end
end)

--Mouse released
Mouse.Button1Up:Connect(function()
	mouseDown = false
	CoolDown = true
	if ChargePower == 0 then
		animtrack:Stop()
		game.ReplicatedStorage.SwordAttack:FireServer(ChargePower)
		wait(3)
		CoolDown = false
	elseif ChargePower == 1 then
		script.Parent.Handle.Swing:Play()
		animtrack:Stop()
		LightHitTrack:Play()
		game.ReplicatedStorage.SwordAttack:FireServer(ChargePower)
		ChargePower = 0
		wait(3)
		CoolDown = false
	elseif ChargePower == 2 then
		script.Parent.Handle.Swing:Play()
		animtrack:Stop()
		LightHitTrack:Play()
		game.ReplicatedStorage.SwordAttack:FireServer(ChargePower)
		ChargePower = 0
		wait(3)
		CoolDown = false
	elseif ChargePower == 3 then
		script.Parent.Handle.Swing:Play()
		animtrack:Stop()
		HardHitTrack:Play()
		script.Parent.Handle.FullyCharge.Enabled = false
		script.Parent.Handle.FullyCharge2.Enabled = false
		game.ReplicatedStorage.SwordAttack:FireServer(ChargePower)
		ChargePower = 0
		wait(3)
		CoolDown = false
	end
end)

Server script:

local Attacking = false
function TargetPlayer(hit)
	humanoid = hit.Parent.Humanoid
end

script.Parent.Handle.Touched:Connect(TargetPlayer)
	
game.ReplicatedStorage.SwordAttack.OnServerEvent:Connect(function(player, x)
	print(x)
	local Attacking = true
	if x == 0 and Attacking == true then
		humanoid:TakeDamage(0)
		wait(0.5)
		local Attacking = false
	elseif x == 1 and Attacking == true then
		humanoid:TakeDamage(15)
		wait(0.5)
		local Attacking = false
	elseif x == 2 and Attacking == true then
		humanoid:TakeDamage(35)
		wait(0.5)
		local Attacking = false
	elseif x == 3 and Attacking == true then
		humanoid:TakeDamage(50)
		wait(1)
		local Attacking = false
	end
end)

Probably isn’t the best practice, but it work!

1 Like