Feedback on sword script I made

The animations, sword model and slash effected aren’t created by me

Overview:

  • Having the slash effect in a less random spot.
  • I have considered having the sword go on your back when you unequip although I am unsure how I’d go about that.

If anybody knows how I can do the things listed above please let me know!
Thanks to anybody who replies! :smiley:

The code below is inside of a server script located in the starter character scripts!

RaycastHitbox module → Raycast Hitbox 4.01: For all your melee needs!

-- [[ Player Variables ]] --
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")

-- [[ Services ]] --
local Debris = game:GetService("Debris")
local TweenService = game:GetService("TweenService")
local Workspace = game:GetService("Workspace")
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

-- [[ Objects ]] --
local FX_Folder = Workspace:WaitForChild("FX")
local ServerAssets = ServerStorage:WaitForChild("ServerAssets")

-- [[ Modules ]] --
local RaycastHitbox = require(ReplicatedStorage:WaitForChild("RaycastHitboxV3"))

-- [[ Boolean ]] --
local Debounce = false

-- [[ Tables ]] --
local Animations = {
	5860750701;
	5860755185;
	5860756987;
}

-- [[ Events ]] --
Character.ChildAdded:Connect(function(chil)
	if chil:IsA("Tool") then
		
		local Handle = chil:WaitForChild("Handle")
		local Hitbox = RaycastHitbox:Initialize(Character, {Character})
		
		Hitbox.OnHit:Connect(function(hit, humanoid)
			if humanoid.Health <= 0 then return end
			
			humanoid:TakeDamage(10)
			
			local directions = {"25","-25"}
			local effect = ServerAssets:WaitForChild("Slash"):clone()
			effect.BrickColor = BrickColor.new("Terra Cotta")
			effect.Material = Enum.Material.Neon
			effect.Transparency = .6
			effect.CanCollide = false
			effect.Anchored = true
			effect.CFrame = hit.CFrame * CFrame.new(
				math.random(10,20)/10,
				math.random(10,20)/10,
				math.random(10,20)/10
			)
			effect.CFrame = hit.CFrame * CFrame.Angles(
				0,
				math.ceil(Character.PrimaryPart.Orientation.Y*90*math.pi^2),
				math.rad(tonumber(directions[math.random(1,#directions)]))
			)
			
			effect.Parent = FX_Folder
			
			local Info = TweenInfo.new(
				1,
				Enum.EasingStyle.Linear,
				Enum.EasingDirection.Out,
				0,
				false,
				.2
			)
			
			local Tween = TweenService:Create(effect, Info, {Transparency = 1})
			Tween:Play()
			Debris:AddItem(Info.Time, effect)
		end)
		
		chil.Activated:Connect(function()
			
			local Acceptable = chil.Parent == Character and not Debounce
			if Acceptable then
				
				Debounce = true
				
				Hitbox:HitStart()
				
				local Swing_Audio = Handle:FindFirstChild("Swing_Audio") or Instance.new("Sound")
				Swing_Audio.Parent = Handle
				Swing_Audio.Name = "Swing_Audio"
				Swing_Audio.Volume = .5
				Swing_Audio.SoundId = "rbxassetid://1306070008"
				Swing_Audio:Play()
				
				local Animation = Instance.new("Animation")
				Animation.AnimationId = ("rbxassetid://%d"):format(tostring(Animations[math.random(1, #Animations)]))
				local AnimationTrack = Humanoid:LoadAnimation(Animation)
				AnimationTrack:Play()
				
				local function Stop(time)
					wait(time)
					Hitbox:HitStop()
					Animation:remove()
					AnimationTrack:remove()
					Debounce = false
				end
				
				local thread = coroutine.wrap(Stop)
				thread(AnimationTrack.Length)
				
			end
			
		end)
	end
end)
2 Likes

You should try using coroutine instead of delay(although not much of a difference), and disconnect your functions(to reduce memory usage). I believe(correct me if I’m wrong), that cloning is much better than instancing.

1 Like

Thanks for the feedback, I’ll make sure to make those changes!

I used the coroutine and it worked! Although I do have a question. Do coroutines use memory and if so how would I disconnect them?

How did you learn this much math? I need help with it.