Highlight client side issue

Issue, when I die the highlight returns false, I want it stay highlighted if it was enabled when I die and respawn. It works for the other players but not yourself.

Video:

local script:

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 createHighlight1(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.90
	highlight.Color3 = Color3.fromRGB(85, 255, 127)
	highlight.Name = "highlight"
	highlight.Parent = part
	return highlight
end

local function removeHighlights1()
	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 highlightPlayer1()
	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
						createHighlight1(part)
					end
				end
			end
		end
	end
end

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

removeHighlights1()

RunService.Heartbeat:Connect(function()
	if highlightingEnabled then
		highlightPlayer1()
		print(highlightingEnabled)
	else
		removeHighlights1()
		print(highlightingEnabled)
	end
end)
1 Like

Is this script parented to the CharacterScripts or PlayerScripts?
Also, putting a Highlight instance into a Model will automatically highlight its descendants

1 Like

it is in characterscripts and I know about the highlight instance

1 Like

If it is in characterscripts then the script gets ran every time the player character is loaded (or spawned).

This line between the MouseButton1Click and Heartbeat connections here inside the script removes the highlight

I’m not sure why this function is being called.

Also, I think the code would be better placed within StarterPlayerScripts since that would only run once when the player joins the game. Tell me if you have a reason for it not being there.

More context for what line I’m talking about in my reply above

That worked, I’m always confused on whens the best time to use startcharacter or starterplayer tbh

2 Likes

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