Need help with Highlight state issue

How would one save the state of the highlight so that if your character dies/respawns it will reapply it when you respawn?

script:

local player = game.Players.LocalPlayer
local highlightingEnabled = false
local toggleSound = game.ReplicatedStorage.Sounds.Click

local playerGui = player.PlayerGui

local settingsGui = playerGui:WaitForChild("SettingsGui")
local frame = settingsGui:WaitForChild("Frame")
local toggleHighlight = frame:WaitForChild("ToggleHighlight")

local function createHighlight(part)
	local highlight = Instance.new("BoxHandleAdornment")
	highlight.Size = part.Size + Vector3.new(0.1, 0.1, 0.1)
	highlight.Adornee = part
	highlight.AlwaysOnTop = true
	highlight.ZIndex = 5
	highlight.Transparency = 0.5
	highlight.Color3 = Color3.fromRGB(85, 255, 127)
	highlight.Parent = part
	return highlight
end

local function removeHighlights()
	highlightingEnabled = false
	for _, player in ipairs(game.Players:GetPlayers()) do
		if player ~= game.Players.LocalPlayer then
			
			local character = player.Character
			
			if character then
				for _, part in ipairs(character:GetDescendants()) do
					if part:IsA("BasePart") then
						for _, highlight in ipairs(part:GetChildren()) do
							if highlight:IsA("BoxHandleAdornment") then
								highlight:Destroy()
							end
						end
					end
				end
			end
		end
	end
end

local function highlightPlayers()
	highlightingEnabled = true
	for _, player in ipairs(game.Players:GetPlayers()) do
		if player ~= game.Players.LocalPlayer then
			
			local character = player.Character
			
			if character then
				for _, part in ipairs(character:GetDescendants()) do
					if part:IsA("BasePart") then
						createHighlight(part)
					end
				end
			end
		end
	end
end

toggleHighlight.MouseButton1Click:Connect(function()
	toggleSound:Play()
	if highlightingEnabled == false then
		highlightPlayers()
		print("Enabled")
	elseif highlightingEnabled == true then
		removeHighlights()
		print("Disabled")
	end
end)

removeHighlights() -- default setting

I don’t care to save it when you leave the game, fyi

Change the script to this:

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

local localPlayer = Players.LocalPlayer
local highlightingEnabled = false
local toggleSound = game.ReplicatedStorage.Sounds.Click

local playerGui = localPlayer.PlayerGui

local settingsGui = playerGui:WaitForChild("SettingsGui")
local frame = settingsGui:WaitForChild("Frame")
local toggleHighlight = frame:WaitForChild("ToggleHighlight")

local function createHighlight(part)
	if part:FindFirstChild("highlight") then return part:FindFirstChild("highlight") end
	local highlight = Instance.new("BoxHandleAdornment")
	highlight.Size = part.Size + Vector3.new(0.1, 0.1, 0.1)
	highlight.Adornee = part
	highlight.AlwaysOnTop = true
	highlight.ZIndex = 5
	highlight.Transparency = 0.5
	highlight.Color3 = Color3.fromRGB(85, 255, 127)
	highlight.Name = "highlight"
	highlight.Parent = part
	return highlight
end

local function removeHighlights()
	for _, player in ipairs(game.Players:GetPlayers()) do
		if player ~= localPlayer then
			local character = player.Character
			if character then
				for _, part in ipairs(character:GetDescendants()) do
					if part:IsA("BasePart") then
						for _, highlight in ipairs(part:GetChildren()) do
							if highlight:IsA("BoxHandleAdornment") then
								highlight:Destroy()
							end
						end
					end
				end
			end
		end
	end
end

local function highlightPlayers()
	for _, player in ipairs(Players:GetPlayers()) do
		if player ~= localPlayer then
			local character = player.Character
			if character then
				for _, part in ipairs(character:GetDescendants()) do
					if part:IsA("BasePart") then
						createHighlight(part)
					end
				end
			end
		end
	end
end

toggleHighlight.MouseButton1Click:Connect(function()
	toggleSound:Play()
	highlightingEnabled = not highlightingEnabled
end)

removeHighlights()

RunService.Heartbeat:Connect(function()
	if highlightingEnabled then
		highlightPlayers()
	else
		removeHighlights()
	end
end)

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