How can i disable a certain part of this script?

this is a local script

player.CharacterAdded:Connect(function(char)
	char:WaitForChild("Humanoid").Died:Connect(function()
		if true then
			local titleScroll = player:WaitForChild("PlayerGui"):WaitForChild("TitlesGui"):WaitForChild("TitlesFrame"):WaitForChild("TitlesScroll")
			titleScroll:WaitForChild("StarterTitleFrame"):WaitForChild("EquipButton").MouseButton1Click:Connect(function()
				if true then
					local leaderstats = player.leaderstats
					if leaderstats.kills.Value > 0 and leaderstats.wins.Value > 0 then
						local starterTitle = titles.StarterTitleGui:Clone()
						local originalTitle = char:WaitForChild("Head"):FindFirstChildWhichIsA("BillboardGui")
						if originalTitle then
							originalTitle:Destroy()
						end
						starterTitle.Parent = char:WaitForChild("Head")
						player.CharacterAdded:Connect(function(newChar)
							if true then
								task.wait(0.5)
								local originalTitle2 = newChar:WaitForChild("Head"):FindFirstChildWhichIsA("BillboardGui")
								if originalTitle2 then
									originalTitle2:Destroy()
								end
								titles.StarterTitleGui:Clone().Parent = newChar:WaitForChild("Head")
							end
						end)
						titleScroll.StarterTitleFrame.EquipButton.Text = "Equipped"
						task.wait(3)
						titleScroll.StarterTitleFrame.EquipButton.Text = "Equip"
					else
						player.PlayerGui.AntiEquipGui.Enabled = true
						task.wait(3)
						player.PlayerGui.AntiEquipGui.Enabled = false
					end
				end
			end)
		end
	end)
end)

i want everything after the 2nd characteradded to disable when the humanoid dies. i have another script almost exactly like this, with minor changes, and right now the 2nd characteraddeds of both are overlapping and breaking the script. i know i can put a humanoid.Died, but i dont know what else to do. please help

4 Likes

a nitpicky thing but
if true then
always runs

and it’s bad practice to wrap gui events

oh and, ‘titles’ is undefined so the script would error anyway
-------------–----------------------------------------------------------------------

from what I’m seeing, you’d probably want to use connections

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local starterTitleFrame = player:WaitForChild("PlayerGui"):WaitForChild("TitlesGui"):WaitForChild("TitlesFrame"):WaitForChild("TitlesScroll"):WaitForChild("StarterTitleFrame")
local equipButton = starterTitleFrame:WaitForChild("EquipButton")
local connection
local connection2
local connection3
local dead = false

function respawn(newcharacter)
character = newcharacter
connection2 = character:WaitForChild("Humanoid").Died:Connect(death)
if not dead then
connection:Disconnect()
connection = player.CharacterAdded:Connect(respawn)
return
end
dead = false
task.wait(0.5)
local originalTitle = character.Head:FindFirstChildWhichIsA("BillboardGui") or false
if originalTitle then originalTitle:Destroy() end
titles.StarterTitleGui:Clone().Parent = character.Head
connection:Disconnect()
connection = player.CharacterAdded:Connect(respawn)
end

function death()
dead = true
connection2:Disconnect()
end

function click()
local leaderstats = player.leaderstats
if leaderstats.kills.Value > 0 and leaderstats.wins.Value > 0 then
local originalTitle = character.Head:FindFirstChildWhichIsA("BillboardGui") or false
if originalTitle then originalTitle:Destroy() end
titles.StarterTitleGui:Clone().Parent = character.Head
else
player.PlayerGui.AntiEquipGui.Enabled = true
task.wait(3)
player.PlayerGui.AntiEquipGui.Enabled = false
end
connection3:Disconnect()
connection3 = equipButton.MouseButton1Click:Connect(click)
end

connection = player.CharacterAdded:Connect(respawn)
connection2 = character:WaitForChild("Humanoid").Died:Connect(death)
connection3 = equipButton.MouseButton1Click:Connect(click)

sorry for no formatting, typed this on mobile

2 Likes

now it just equips the title when i respawn, only when i respawn, no matter what, even if i dont press the equip button.

i tried to add some code to fix that but nothing worked, in fact, it always broke the script even more

You can create a function and when the player dies you can just clone and delete these scripts. You can put this manager script into StarterPlayerScripts.

how would i delete the scripts though? sorry, im a beginner scripter

unless you mean to just delete the entire script completely, but im a bit confused

added a debounce, now the script works perfectly

2 Likes

Of course you don’t need to do that! Basically you can use different scripts instead of only one script for different functions you need to run. Also you can delete the script by using script:Destroy() and clone it with script:Clone(). Also you should clone it before you destroy it.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.