Highlight issue, server and client

issue, so I created a highlight script that works on the server and client side, the issue is my player is being highlighted when toggled. I only want to see other players highlighted, not me.

client script:

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

local playerGui = player.PlayerGui

local settingsGui = playerGui:WaitForChild("HighlightServerGui")
local toggleHighlight = settingsGui:WaitForChild("ToggleHighlight")

local highlightingStateKey = "HighlightingState"
local toggleHighlightEvent = game.ReplicatedStorage.ToggleHighlightEvent

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

local function saveHighlightingState()
	game:GetService("DataStoreService"):GetDataStore("PlayerData"):SetAsync(player.UserId .. highlightingStateKey, highlightingEnabled)
end

local function loadHighlightingState()
	local dataStore = game:GetService("DataStoreService"):GetDataStore("PlayerData")
	local success, state = pcall(function()
		return dataStore:GetAsync(player.UserId .. highlightingStateKey)
	end)
	if success and state ~= nil then
		highlightingEnabled = state
		if highlightingEnabled then
			highlightPlayers()
		end
	end
end

toggleHighlight.MouseButton1Click:Connect(function()
	toggleSound:Play()
	highlightingEnabled = not highlightingEnabled
	toggleHighlightEvent:FireServer(highlightingEnabled)
	if highlightingEnabled then
		highlightPlayers()
		print("Enabled")
	else
		removeHighlights()
		print("Disabled")
	end
end)

function highlightState()
	loadHighlightingState()
end

local function onCharacterAdded(character)
	if highlightingEnabled then
		highlightPlayers()
	end
	saveHighlightingState()
end

removeHighlights() -- default setting
highlightState() -- load highlighting state when script starts

toggleHighlightEvent.OnClientEvent:Connect(function(highlightingEnabled)
	if highlightingEnabled then
		highlightPlayers()
		print("Enabled")
	else
		removeHighlights()
		print("Disabled")
	end
end)

server script:

local DataStoreService = game:GetService("DataStoreService")
local PlayerDataStore = DataStoreService:GetDataStore("PlayerData")

local toggleHighlightEvent = game.ReplicatedStorage.ToggleHighlightEvent
local highlightingStateKey = "HighlightingState"


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()
	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()
	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

local function saveHighlightingState(player, highlightingEnabled)
	local success, errorMessage = pcall(function()
		PlayerDataStore:SetAsync(player.UserId .. highlightingStateKey, highlightingEnabled)
		warn("Saved highlighting state for player " .. player.Name)
	end)
	if not success then
		warn("Error saving highlighting state for player " .. player.Name .. ": " .. errorMessage)
	end
end

local function loadHighlightingState(player)
	local success, state = pcall(function()
		return PlayerDataStore:GetAsync(player.UserId .. highlightingStateKey)
	end)
	if success and state ~= nil then
		toggleHighlightEvent:FireClient(player, state)
	end
end

toggleHighlightEvent.OnServerEvent:Connect(function(player, highlightingEnabled)
	if highlightingEnabled then
		highlightPlayers()
	else
		removeHighlights()
	end
	saveHighlightingState(player, highlightingEnabled)
end)

game.Players.PlayerAdded:Connect(function(player)
	loadHighlightingState(player)
end)

I’m also getting an error:

any help is appreciated!

Create and save highlights only on the server, as they will replicate to the client. Make the client load the appropriate highlights when needed, (like a loop or something). Excess code (creating highlights on the client and saving on the client which caused the error, etc.) can be deleted

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