Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How can I make the sword ability only play when being damaged?

I am revamping a SFOTH sword, which Is the GhostWalker but what I like to call It: "Ghost Eye" The ability Is to make you completely Invisible when taking damage, but after 5 seconds or when the tool has been equip It makes you slightly Invisible, the tool works but I realized that when taking damage or healing It still fires the ability

Script:

local Tool = script.Parent
local SoundFolder = Tool.Sounds
local SlashAttack
local Boolens = {
	Debounce = false,
	CanDamage = false
}
local Animations = { ---Oh boi there Is alot
	EquipAnimation = Tool.EquipAnimation,
	Idle = Tool.Idle,
	Slash = Tool.Slash
}

Tool.Equipped:Connect(function()
	SoundFolder.Unsheath:Play()
	local Humanoid = Tool.Parent.Humanoid
	Humanoid.WalkSpeed = 20
	Tool.Handle.Transparency = 0.9
	Humanoid.Parent.Head.Transparency = 0.9
	Humanoid.Parent.Torso.Transparency = 0.9
	Humanoid.Parent["Left Arm"].Transparency = 0.9
	Humanoid.Parent["Right Arm"].Transparency = 0.9
	Humanoid.Parent["Left Leg"].Transparency = 0.9
	Humanoid.Parent["Right Leg"].Transparency = 0.9
	
	local EquipAnimationTrack = Humanoid:FindFirstChild("Animator"):LoadAnimation(Animations.EquipAnimation)
	EquipAnimationTrack:Play()
	task.wait(0.30)
	local Idle = Humanoid:FindFirstChild("Animator"):LoadAnimation(Animations.Idle)
	Idle:Play()
	Humanoid.HealthChange:Connect(function()
		Humanoid.WalkSpeed = 30
		Tool.Handle.Transparency = 1
		Humanoid.Parent.Head.Transparency = 1
		Humanoid.Parent.Torso.Transparency = 1
		Humanoid.Parent["Left Arm"].Transparency = 1
		Humanoid.Parent["Right Arm"].Transparency = 1
		Humanoid.Parent["Left Leg"].Transparency = 1
		Humanoid.Parent["Right Leg"].Transparency = 1
		wait(5)Humanoid.WalkSpeed = 20
		Tool.Handle.Transparency = 0.9
		Humanoid.Parent.Head.Transparency = 0.9
		Humanoid.Parent.Torso.Transparency = 0.9
		Humanoid.Parent["Left Arm"].Transparency = 0.9
		Humanoid.Parent["Right Arm"].Transparency = 0.9
		Humanoid.Parent["Left Leg"].Transparency = 0.9
		Humanoid.Parent["Right Leg"].Transparency = 0.9
	end)
	Tool.Unequipped:Connect(function()
		Humanoid.WalkSpeed = 16
		Tool.Handle.Transparency = 0.5
		Humanoid.Parent.Head.Transparency = 0
		Humanoid.Parent.Torso.Transparency = 0
		Humanoid.Parent["Left Arm"].Transparency = 0
		Humanoid.Parent["Right Arm"].Transparency = 0
		Humanoid.Parent["Left Leg"].Transparency = 0
		Humanoid.Parent["Right Leg"].Transparency = 0
		Idle:Stop()
	end)
end)


Tool.Activated:Connect(function()
	if not Boolens.Debounce then
		Boolens.Debounce = true
		Boolens.CanDamage = true
		SoundFolder.Slash:Play()
		print("The tool has been clicked")
		local Humanoid = Tool.Parent.Humanoid

		SlashAttack = Humanoid:FindFirstChild("Animator"):LoadAnimation(Animations.Slash)
		SlashAttack:Play()
		wait(1)
		Boolens.Debounce = false
		Boolens.CanDamage = false
	end
end)

Tool.Handle.Touched:Connect(function(hit)
	local Player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
	if Player then
		local player1 = game.Players:GetPlayerFromCharacter(Tool.Parent)
		local player2 = game.Players:GetPlayerFromCharacter(hit.Parent)

		if (player1 ~= nil) and (player2 ~= nil) then
			local humanoid = (player2.Character or player2.CharacterAdded:Wait()):FindFirstChildWhichIsA("Humanoid")

			if (player1.TeamColor ~= player2.TeamColor) then
				if (humanoid ~= nil) and Boolens.CanDamage == true then
					Boolens.CanDamage = false
					humanoid:TakeDamage(10)
					humanoid.WalkSpeed = 10
					humanoid.JumpPower = 30
					wait(5)
					humanoid.WalkSpeed = 16
					humanoid.JumpPower = 50
				end
			end
		end
	else
		local NPCHumanoid = hit.Parent:WaitForChild("Humanoid")
		if NPCHumanoid and Boolens.CanDamage == true then
			Boolens.CanDamage = false
			NPCHumanoid:TakeDamage(10)
			NPCHumanoid.WalkSpeed = 10
			NPCHumanoid.JumpPower = 30
			wait(5)
			NPCHumanoid.WalkSpeed = 16
			NPCHumanoid.JumpPower = 50
		end
	end
end)


--[[Facts:
Originally made you Invisible when damaged, even the original tooltip tells you this:
A ghost with algophobia formed Into a sword, thats why It makes you Invisible when being damaged
]]

1 answer

This one is pretty inefficient,first give all the swords codes that detect player name when they hit someone (in enemy team of course),then check all players and see if they have the sword and the name

this is the script in case you need in all swords scripts,recomended in line that you make enemy player get damage:

local name = player2.Name for i,v in pairs(game.Players:GetPlayers()) do for _,p in pairs(v:FindFirstChild(name)) do if p:FindFirstChild("tool here") then -- do the increase stuffs here end end end

i write this on web so i didnt test it out,hope the script will work for you

This discussion is preserved as it was. New answers, comments, and votes are closed.