Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Player left with highlight after disabled?

So basically i have code where if you hold down q then you can see players through walls using the highlight feature that has been created recently but my problem is where when its disabled one of the dummys is still left using it now i tested it with more characters it just seems to iterate through twice and not more

local UserInputService = game:GetService("UserInputService")
local CurrentCamera = workspace.CurrentCamera

local Keycode = Enum.KeyCode.Q 

local ActivationSound = script:WaitForChild("SoundEffect")

local FillColor = Color3.fromRGB(28, 255, 43)
local OutlineColor = Color3.fromRGB(13, 13, 13)

function PlayerHeat(Removal)	
	for i, model in pairs(workspace:GetChildren()) do
		if model:FindFirstChild("Humanoid") and model ~= script.Parent then
			local Character = model
			
			if Removal and Character:FindFirstChildOfClass("Highlight") then
				for i, model in pairs(workspace:GetChildren()) do
					if model:FindFirstChild("HumanoidRootPart") then
						print(model)
						print(model:FindFirstChildOfClass("Highlight"))

						model:FindFirstChildOfClass("Highlight"):Destroy()
						if not game.Lighting:FindFirstChild("HeatCorrection") then return end
						game.Lighting:FindFirstChild("HeatCorrection"):Destroy()
					end
				end
			end
			
			local NewHighlight = Instance.new("Highlight")
			
			NewHighlight.Adornee = Character
			NewHighlight.DepthMode = Enum.HighlightDepthMode.AlwaysOnTop
			NewHighlight.Enabled = true
			NewHighlight.FillColor = FillColor
			NewHighlight.OutlineColor = OutlineColor
			NewHighlight.FillTransparency = 0
			NewHighlight.OutlineTransparency = 0
			
			NewHighlight.Parent = Character
		end
	end
	local CorrectionClone = script.ColorCorrection:Clone()
	CorrectionClone.Name = "HeatCorrection"
	CorrectionClone.Parent = game.Lighting
end

function FieldOfViewMod(FOV)
	CurrentCamera.FieldOfView = FOV
end

UserInputService.InputBegan:Connect(function(input, processed)
	if processed then return end
	
	if input.KeyCode == Keycode then
		PlayerHeat(false)
		FieldOfViewMod(45)
		ActivationSound:Play()
	end
end)

UserInputService.InputEnded:Connect(function(input, processed)
	if processed then return end
	
	if input.KeyCode == Keycode then
		PlayerHeat(true)
		FieldOfViewMod(70)
		return
	end
end)

1 answer

✓ Accepted answer

Hello, ragdollkiiing2!

The only problem with the code is that when removing, you are using return if there is no HeatCorrection in lighting, which is probably what is breaking the code, as it works as a break for the loop.

To fix it, you can replace it with continue, which just stops current iteration on the loop

local UserInputService = game:GetService("UserInputService")
local CurrentCamera = workspace.CurrentCamera

local Keycode = Enum.KeyCode.Q 

local ActivationSound = script:WaitForChild("SoundEffect")

local FillColor = Color3.fromRGB(28, 255, 43)
local OutlineColor = Color3.fromRGB(13, 13, 13)

function PlayerHeat(Removal)    
    for i, model in pairs(workspace:GetChildren()) do
        if model:FindFirstChild("Humanoid") and model ~= script.Parent then
            local Character = model

            if Removal and Character:FindFirstChildOfClass("Highlight") then
                for i, model in pairs(workspace:GetChildren()) do
                    if model:FindFirstChild("HumanoidRootPart") then
                        print(model)
                        print(model:FindFirstChildOfClass("Highlight"))

                        model:FindFirstChildOfClass("Highlight"):Destroy()
                        if not game.Lighting:FindFirstChild("HeatCorrection") then continue end
                        game.Lighting:FindFirstChild("HeatCorrection"):Destroy()
                    end
                end
            end

            local NewHighlight = Instance.new("Highlight")

            NewHighlight.Adornee = Character
            NewHighlight.DepthMode = Enum.HighlightDepthMode.AlwaysOnTop
            NewHighlight.Enabled = true
            NewHighlight.FillColor = FillColor
            NewHighlight.OutlineColor = OutlineColor
            NewHighlight.FillTransparency = 0
            NewHighlight.OutlineTransparency = 0

            NewHighlight.Parent = Character
        end
    end
    local CorrectionClone = script.ColorCorrection:Clone()
    CorrectionClone.Name = "HeatCorrection"
    CorrectionClone.Parent = game.Lighting
end

function FieldOfViewMod(FOV)
    CurrentCamera.FieldOfView = FOV
end

UserInputService.InputBegan:Connect(function(input, processed)
    if processed then return end

    if input.KeyCode == Keycode then
        PlayerHeat(false)
        FieldOfViewMod(45)
        ActivationSound:Play()
    end
end)

UserInputService.InputEnded:Connect(function(input, processed)
    if processed then return end

    if input.KeyCode == Keycode then
        PlayerHeat(true)
        FieldOfViewMod(70)
        return
    end
end)

If this solves your question, please mark it as the Accepted Answer

This discussion is preserved as it was. New answers, comments, and votes are closed.