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

Trying to make a sword that makes you Invisible when damaged, but also fires when healing (?)

Im trying to make a sword that makes you Invisible when damaged, currently It only makes your body parts (R6) Invisible, works but there Is a problem where It will activate the ability when healing but I want It to only work when damaged

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)

1 answer

I believe it's because you're using Humanoid.HelathChange() (on line 31). To fix this, I would just make 2 variables to see the difference in health. Other than that, I do have a suggestion. I would do an in pairs() loop to set the transparency. (I marked all the places I used in pairs() as I made it into a function

-- Starting on line 13 in your script
local function setCharacterTransparency(characterModel, transparencyValue)
	if characterModel and transparencyValue then -- prevents script from breaking if not all parameters are given
		for _, object in pairs(characterModel)
			if object:IsA("BasePart") then -- checks to see if the child is a part (there is the humanoid, default scripts from roblox, animator, etc. that don't have a transparency value)
				object.Transparency = transparencyValue
			end
		end
	end
end

Tool.Equipped:Connect(function()
	SoundFolder.Unsheath:Play()
	local Character = Tool.Parent -- I added this variable for the in pairs() loop
	local Humanoid = Tool.Parent.Humanoid
	Humanoid.WalkSpeed = 20
	Tool.Handle.Transparency = 0.9
	setCharacterTransparency(Character, 0.9) -- in pairs() (much cleaner, right?)
	local EquipAnimationTrack = Humanoid:FindFirstChild("Animator"):LoadAnimation(Animations.EquipAnimation)
	EquipAnimationTrack:Play()
	task.wait(0.30)
	local Idle = Humanoid:FindFirstChild("Animator"):LoadAnimation(Animations.Idle)
	Idle:Play()
	local oldHealth = Humanoid.Health
	Humanoid.HealthChange:Connect(function()
		local newHealth = Humanoid.Health
		if newHealth < oldHealth then -- here, I check to see if the character's current health is lower than their health before
			Humanoid.WalkSpeed = 30
			Tool.Handle.Transparency = 1
			setCharacterTransparency(Character, 1) -- in pairs()
			wait(5)Humanoid.WalkSpeed = 20
			Tool.Handle.Transparency = 0.9
			setCharacterTransparency(Character, 0.9) -- in pairs()
		end
		oldHealth = newHealth -- make sure to reset variable (updating oldHealth)
	end)
	Tool.Unequipped:Connect(function()
		Humanoid.WalkSpeed = 16
		Tool.Handle.Transparency = 0.5
		setCharacterTransparency(Character, 0) -- in pairs()
		Idle:Stop()
	end)
end) -- Ending on line 60 in your script

If you'd like to add the tool transparency and walkspeed (Replace the setCharacterTransparency() function):

local function invisibilityToolFunction(characterModel, transparencyValue, walkSpeed, tool) -- change the function's name to be whatever you'd like
	if characterModel then
		if walkspeed then
			characterModel.Humanoid.WalkSpeed = walkSpeed
		end
		if transparencyValue then
			if tool then
				tool.Handle.Transparency = transparencyValue
			end
			for _, object in pairs(characterModel)
				if object:IsA("BasePart") then
					object.Transparency = transparencyValue
				end
			end
		end
	end
end

3 comments

0

I also see that you set the tool's transparency and walkspeed whenever you change the player's visibility, you could also add those into the function (just make more parameters and possibly rename the function for clarity).

0

Where should I place the script?

0

This is replacing your script, I specified the start and end.

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