I used ChatGPT for a specific local script that worked for what I asked -- Is it efficient?

After fiddling around with ChatGPT for about an hour, I managed to create a script that did the criteria I asked:

Using Roblox Studio’s scripting language, LUA, please make a client sided script that enables a specifically named colorcorrection whenever a Roblox Character is inside the size of a part that is named the same as the colorcorrection. Make it so the specifically named parts has its own folder in workspace.

For example: A player entering a part named “green” which is within a folder in the Roblox Workspace named “ColorCorrectionParts” will enable the color correction in Lighting named “green”. Also, make it so when the player leaves the part, it disables the specifically named ColorCorrection. Have it so the script works with StarterCharacterScripts and NOT within a part.

I feel like this is relatively simple to just enable a feature if you walk inside of a part, like if I were to make it locked or anchored, but I’m also not sure because I’m not a scripter! Here is the script provided from chatgpt, if there was a more simpler way, I’d like to know :+1:

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

local player = Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local humanoidRootPart = char:WaitForChild("HumanoidRootPart")
local partsFolder = game.Workspace.ColorCorrectionParts -- Replace with the actual folder name containing your trigger parts

local function enableColorCorrection(correctionName)
    local colorCorrection = Lighting:FindFirstChild(correctionName)
    if colorCorrection and colorCorrection:IsA("ColorCorrectionEffect") then
        colorCorrection.Enabled = true
    end
end

local function disableColorCorrection(correctionName)
    local colorCorrection = Lighting:FindFirstChild(correctionName)
    if colorCorrection and colorCorrection:IsA("ColorCorrectionEffect") then
        colorCorrection.Enabled = false
    end
end

local function updateColorCorrection()
    local characterPosition = humanoidRootPart.Position
    local activeColorCorrection = nil
    
    for _, part in ipairs(partsFolder:GetChildren()) do
        if part:IsA("Part") then
            local partSize = part.Size
            local partPosition = part.Position

            local minX = partPosition.X - partSize.X / 2
            local maxX = partPosition.X + partSize.X / 2
            local minY = partPosition.Y - partSize.Y / 2
            local maxY = partPosition.Y + partSize.Y / 2
            local minZ = partPosition.Z - partSize.Z / 2
            local maxZ = partPosition.Z + partSize.Z / 2

            if characterPosition.X >= minX and characterPosition.X <= maxX and
               characterPosition.Y >= minY and characterPosition.Y <= maxY and
               characterPosition.Z >= minZ and characterPosition.Z <= maxZ then
                activeColorCorrection = part.Name
                break
            end
        end
    end
    
    for _, part in ipairs(partsFolder:GetChildren()) do
        if part:IsA("Part") then
            if part.Name == activeColorCorrection then
                enableColorCorrection(part.Name)
            else
                disableColorCorrection(part.Name)
            end
        end
    end
end

RunService.RenderStepped:Connect(function()
    updateColorCorrection()
end)

4 Likes

not in the slightest, use spatial query for this.

5 Likes

You can use ForeverHD’s ZonePlus module to achieve what you’re trying to do.

4 Likes

Instead of checking if there’s a player in the folder every single physics calc, use the Zone+ module

2 Likes

How is this better/more “efficient”? You guys are telling me to add a module script with 11 more module scripts added on to it. It’s like adding a whole admin system just to fly in a game

2 Likes

While it might have more “bloat” it’s more efficient since it uses spatial query and it’s a bit more versatile.

6 Likes

It’s one main module with smaller components that handle certain aspects of it (cleanup, signals, etc.). Behind the scenes, it’s doing all the heavy lifting and that definitely makes your life easier.

5 Likes

This is not efficient at all, because it’s looping through every part every single frame. It’s also using :FindFirstChild many times, and also very unnecessary math. I would use .Touched events instead.

Code:
(make sure to put this in StarterPlayerScripts, putting it in StarterCharacterScripts is unnecessary)

--//Services
local Players = game:GetService("Players")
local Lighting = game:GetService("Lighting")

--//Variables
local LocalPlayer = Players.LocalPlayer
local partsFolder = workspace.ColorCorrectionParts -- Replace with the actual folder name containing your trigger parts

--//Functions
local function initializeParts()
	for _, part in ipairs(partsFolder:GetChildren()) do
		if part:IsA("BasePart") then
			local colorCorrection = Lighting:WaitForChild(part.Name)
			
			part.Touched:Connect(function(hit)
				if LocalPlayer.Character and hit.Parent == LocalPlayer.Character then
					colorCorrection.Enabled = true
				end
			end)
			
			part.TouchEnded:Connect(function(hit)
				if LocalPlayer.Character and hit.Parent == LocalPlayer.Character then
					colorCorrection.Enabled = false
				end
			end)
		end
	end
end

initializeParts()

Keep in mind you could probably optimize it more, but I just optimized it enough so that it won’t lag if there are more parts. Also, I made these optimizations in like 2 minutes so if there are any bugs, tell me.

2 Likes