Optimizing script that Highlight player with certain AssemblyLinearVelocity

Hello, I made this script, but i really think that is too poorly optimized, and I don’t really know a way to make it more optimized

Not moving:
image

Moving:

Any help is welcome c:

local character = script.Parent.Parent.Parent
local velocity = character.HumanoidRootPart.AssemblyLinearVelocity.Magnitude;
local RunService = game:GetService("RunService")
local Tween = game:GetService("TweenService")
--local maxvelocity = Vector3.new(0, 0, 16.5)
local maxvelocity = 20

local players = game.Teams.Survivors:GetPlayers()

local player = game.Players:GetPlayerFromCharacter(character)
local gui = player.PlayerGui.Game_Gui.ExposeUI

local anim = require(game.ReplicatedStorage.GuiAnimations)

local Outline_Show = { OutlineTransparency = 0}
local Outline_Invisible = { OutlineTransparency = 1}



RunService.Stepped:Connect(function()
if #players < 3 then
if velocity < maxvelocity then
		local highlight = script.Parent.Parent.Parent.Highlight
			highlight.Adornee = character
			highlight.OutlineColor = Color3.new(1, 1, 1)
		--highlight.OutlineTransparency = 0
		Tween:Create(highlight, TweenInfo.new(10, Enum.EasingStyle.Back, Enum.EasingDirection.Out, 0, false, 0), Outline_Show):Play()
	if character.HumanoidRootPart.AssemblyLinearVelocity.Magnitude > maxvelocity then
	local highlight = script.Parent.Parent.Parent.Highlight
			Tween:Create(highlight, TweenInfo.new(1, Enum.EasingStyle.Back, Enum.EasingDirection.Out, 0, false, 0), Outline_Invisible):Play()
			end
			end
	end
end)

I dont know much about performance optimization but i do know that it is faster to store a function return as a variable once rather than call it every time you need it (if its constant). this includes indexing the parents or children of an instance

so i would replace your loop with this:

local highlight = script.parent.Parent.Parent.Highlight
local color = Color3.new(1,1,1)
local showTween = Tween:Create(highlight, TweenInfo.new(10, Enum.EasingStyle.Back, Enum.EasingDirection.Out, 0, false, 0), Outline_Show)
local invisibleTween = Tween:Create(highlight, TweenInfo.new(1, Enum.EasingStyle.Back, Enum.EasingDirection.Out, 0, false, 0), Outline_Invisible)

RunService.Stepped:Connect(function()
if #players >= 3 then return end

if character.HumanoidRootPart.AssemblyLinearVelocity.Magnitude <= maxvelocity then
	highlight.Adornee = character
	highlight.OutlineColor = color
	--highlight.OutlineTransparency = 0
	showTween:Play()
else
	invisibleTween:Play()
end
end)

(just replace the RunService.Stepped block, not the whole script)

i also fixed some bugs with your code which is maybe why you were having problems in the first place.

For one, when you define velocity = character.HumanoidRootPart.AssemblyLinearVelocity.Magnitude, this will store the value at the time of the variable being declared, and will never change again. therefore it does not tell you the characters current velocity and so your script would not work at all. you need to index it every time the stepped event fires.

Second, the if statement for their velocity greater than maxvelocity was nested inside the if statement for their velocity being less than max velocity. therefore it would only get to the invisibility tween if their velocity was less than max AND it was greater than max (not possible lol)

anyways see how this works

1 Like

It works really well!
Thanks for the help, actually this solved most problems i had! :sweat_smile:
Now I feel that it works more “faster”, if that make sense somehow.

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