Can you rate my code and tell me about security?

I would really like you to rate my code. I’m also confused by the fact that I placed it in the Tool, can exploiters take advantage of this?

local swordTool = script.Parent
local swordPart = script.Parent.Handle

local attack1Animation = game.ServerStorage.Animations.Attack1Animation
local attack2Animation = game.ServerStorage.Animations.Attack2Animation

local equipmentSound = swordPart.Unsheath
local swingSound = swordPart.SwordSlash
local hitSound= swordPart.SwordLunge

local animation

local isActivated = false
local cooldown = false


swordPart.Touched:Connect(function(part)
	if not isActivated or cooldown then return end
	cooldown = true
	
	local touchedPlayer = game.Players:GetPlayerFromCharacter(part.Parent)
	
	if touchedPlayer then
		
		touchedPlayer.Character.Humanoid:TakeDamage(swordTool.Damage.Value)
		hitSound:Play()
		
		local player = game.Players:GetPlayerFromCharacter(swordTool.Parent)
		if touchedPlayer.Character.Humanoid.Health <= 0 then
			player.leaderstats.Coins.Value += 10
			
			print(player.Name.."was awarded 10 coins for kill")
			
			print(player.Name.." killed "..touchedPlayer.Name)
		end
		
	end
	
	
	task.wait(1)
	cooldown = false
	
end)

swordTool.Equipped:Connect(function()
	equipmentSound:Play()
end)

swordTool.Activated:Connect(function()
	if isActivated then return end
	isActivated = true
	
	
	local animator = swordTool.Parent.Humanoid.Animator
	
	if math.random() >= 0.5 then
		animation = animator:LoadAnimation(attack1Animation)
	else
		animation = animator:LoadAnimation(attack2Animation)
	end
	
	animation:Play()
	swingSound:Play()
	
	task.wait(1)
	
	isActivated = false
end)

If this is a server script then you are good with security, if it’s not then it won’t really work at all, but I suppose this is a server script so you’re good to go. I would also suggest handling the animations on the client as it loads them faster.

I would also suggest using rays to see hits as .Touched is going to be deprecated and it already doesn’t work great.

2 Likes

Source? I know that .Touched often doesn’t work properly but I’ve never heard about it being planned for deprecation.

2 Likes

While it fails in doing a lot of things, I do still find .Touched to be useful on occasion. Are they planning on implementing a suitable replacement?

1 Like