My problem is I don’t want to be highlighted when highlight is enabled aka client sided players.
video:
client:
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
toggleHighlight.MouseButton1Click:Connect(function()
toggleSound:Play()
highlightingEnabled = not highlightingEnabled
toggleHighlightEvent:FireServer(highlightingEnabled)
if highlightingEnabled then
highlightPlayers()
print("Enabled")
else
removeHighlights()
print("Disabled")
end
end)
local function onCharacterAdded(character)
if highlightingEnabled then
highlightPlayers()
end
end
removeHighlights() -- default setting
toggleHighlightEvent.OnClientEvent:Connect(function(highlightingEnabled)
if highlightingEnabled then
highlightPlayers()
print("Enabled")
else
removeHighlights()
print("Disabled")
end
end)
server:
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)
Under the function createHighlight, try adding this:
if part:IsDescendantOf(player.Character) then return end
Or when you put: if player ~= game.Players.LocalPlayer
For some reason instances don’t compare well. Try changing to: if player.UserId ~= game.Players.LocalPlayer.UserId
Or remove that part and put: if player.UserId == game.Players.LocalPlayer.UserId then continue end
Also, I would not recommend using the same variable name for the local player and for each player in the loop.
so I just put whoever press the button doesn’ get highlighted but I can’t figure out how to make it to where if the player dies/respawns the highlight is still enabled for me they are still highlighted and this goes for when new players join.
server:
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
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
local function highlightPlayers(triggeringPlayer)
for _, player in ipairs(game.Players:GetPlayers()) do
if player ~= triggeringPlayer 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(player)
else
removeHighlights()
end
saveHighlightingState(player, highlightingEnabled)
end)
game.Players.PlayerAdded:Connect(function(player)
loadHighlightingState(player)
end)
client:
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 toggleHighlightEvent = game.ReplicatedStorage.ToggleHighlightEvent
toggleHighlight.MouseButton1Click:Connect(function()
toggleSound:Play()
highlightingEnabled = not highlightingEnabled
toggleHighlightEvent:FireServer(highlightingEnabled)
end)
I want to highlight other players besides myself, and have it so if a new player join they get highlighted if highlight is enabled on my end and if a player dies/respawns they’re still highlighted if its enabled on my end.
So, like I said, you only want to remove other player’s highlights right?
If so, you need to fix a few things:
You can’t use .LocalPlayer on the server
You have repeated code
There’s easier solutions than the one you attempted
Server code:
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
end
local function removeHighlights()
for _, player in ipairs(game.Players:GetPlayers()) do
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
local function highlightPlayers(localPlayer)
for _, player in ipairs(game.Players:GetPlayers()) do
if player == localPlayer then
continue
end
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
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)
end
toggleHighlightEvent.OnServerEvent:Connect(function(player, highlightingEnabled)
if highlightingEnabled then
highlightPlayers(player)
else
removeHighlights()
end
saveHighlightingState(player, highlightingEnabled)
end)
game.Players.PlayerAdded:Connect(loadHighlightingState)
Client code:
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
toggleHighlight.MouseButton1Click:Connect(function()
toggleSound:Play()
highlightingEnabled = not highlightingEnabled
toggleHighlightEvent:FireServer(highlightingEnabled)
end)