How do i optimize this script?

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

local StateModule = {}
StateModule.Params = RaycastParams.new()
StateModule.Params.FilterType = Enum.RaycastFilterType.Include
StateModule.Params.IgnoreWater = true
StateModule.Params.FilterDescendantsInstances = {  workspace.Map  }
StateModule.SavedConnections = {}

function StateModule.EnableState(Player)
	task.wait(1.5)
	if not Player.Character then
		    repeat task.wait() until Player.Character
	end
	StateModule.SavedConnections[Player.Name] = {}
	local SavedIndex = StateModule[Player.Name]
	local Folder = Instance.new("Folder", Player)
	Folder.Name = "States"
	Folder:SetAttribute("Beneath", Enum.Material.SmoothPlastic)
	Folder:SetAttribute("ActivateCheck", false)
	SavedIndex["BeneathCheck"] = RunService.Stepped:Connect(function()
		task.wait(0.5)
		if Player.IsGettingDamaged.Value == true or Player.Ragdoll.Value == true or Player.IsGettingDamagedAndBlocking.Value == true or Player.IsGuardBroken.Value == true or Player.AbilityBy.Value ~= "" or Player.IsUsingAbility.Value == true or Folder:GetAttribute("ActivateCheck") == true then
			if not Player.Character then
				return
			end
			local CharacterRaycast = workspace:Raycast(Player.Character.HumanoidRootPart.Position, Vector3.new(0,-4, 0),  StateModule.Params)
			if CharacterRaycast then
				print("Found: "..tostring(CharacterRaycast.Material))
				Folder:SetAttribute("Beneath",  CharacterRaycast.Material)
			else
				Folder:SetAttribute("Beneath", Enum.Material.Air)
			end
		end
	end)
end

return StateModule

How would i optimize this script and make it more efficient?

1 Like

why not just

if not Player.Character then
    Player.CharacterAdded:Wait()
end
2 Likes

To optimize this script:

  1. Avoid Repeated Lookup: Instead of repeatedly looking up Player.Character, store it once in a variable for reuse.
  2. Reduce Wait Time: If you’re waiting for Player.Character to exist, consider using events such as CharacterAdded instead of repeatedly waiting with task.wait().
  3. Remove Unnecessary Waits: There are some unnecessary waits in the code. For example, the wait of 0.5 seconds before checking conditions in the Stepped connection.
  4. Minimize Redundant Checks: Some conditions like Player.IsGettingDamaged.Value == true are checked multiple times. You can simplify these checks.
  5. Avoid String Operations: String operations like concatenation ("Found: "..tostring(CharacterRaycast.Material)) can be avoided when not necessary.
  6. Optimize Raycasting: If possible, optimize the raycasting logic to reduce unnecessary calls or use cached values when appropriate.
1 Like

Copy and pasting from chatgpt3 Is wild

4 Likes

ahahahahahahahahaha, it is true

local StateModule = {}
StateModule.Params = RaycastParams.new()
StateModule.Params.FilterType = Enum.RaycastFilterType.Include
StateModule.Params.IgnoreWater = true
StateModule.Params.FilterDescendantsInstances = {  workspace.Map  }
StateModule.SavedConnections = {}

above becomes below:

local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Include
params.IgnoreWater = true
params.FilterDescendantsInstances = {workspace.Map}

local StateModule = {
    Params = params
    SavedConnections = {}
}

no need for dot indexing when its not needed.

This doesn’t really make a difference in terms of performance, Im simply looking for optimization tips that would help improve the script in terms of performance.