--!strict
--!native
--!optimize 2
local RunService = game:GetService("RunService")
local Heartbeatconnection: RBXScriptConnection? = nil
local characterhitbox: BasePart = nil
local function raycast(vectore: Vector3,vectorr:Vector3,number:number,numbero:number)
--if your function is not just workspace:raycast, then
-- USE vector.create(0,0,0) INSTEAD OF Vector3, BECAUSE VECTOR.CREATE FASTER
return vector.create(0,0,0),vector.create(0,0,0),1
end
local inputconnection: RBXScriptConnection = game.ReplicatedStorage.clientbindableevents.sendInput.Event:Connect(function(inputname: string, timesent: number, activity: boolean)
if os.clock() - timesent > .5 then warn("input sent late") return end
if inputname == "E" then
if activity and (not Heartbeatconnection or not Heartbeatconnection.Connected) then
Heartbeatconnection = RunService.Heartbeat:Connect(function()
local smallestdistance: number? = nil
local smallestnormal: vector? = nil
local smallestposition: vector? = nil
for i = 1, 16, 1 do
local angledegree: number = i * 22.5
local position: vector?, normal: vector?, distance: number? = raycast(characterhitbox.Position, Vector3.new(math.cos(math.rad(angledegree)), 0, math.sin(math.rad(angledegree))))
if distance and (smallestdistance == nil or distance < smallestdistance) then
smallestdistance = distance
smallestposition = position
smallestnormal = normal
end
print(position, normal)
end
end)
elseif not activity and Heartbeatconnection and Heartbeatconnection.Connected then
Heartbeatconnection:Disconnect()
Heartbeatconnection = nil
end
end
end)
it’s likely that my script is wrong and doesn’t work, but the general gist is: if smg == true then
is equivalent to if smg then
however, it is faster by one billionth of a second (relatively speaking). Also if not smg then
instead of if smg == false then
using the --!strict and --!native directives with full code typing should increase speed by almost 20%
–!optimize 2 is also likely to add speed to the code.
check the pressed button in another script! If it turns out not to be an E, then just don’t bindableEvent:Fire(). It will save you extra milliseconds of optimization.
Of course, parallel luau will definitely help you and will be much better than asynchronous
(Promises and Why You Should Use Them)
Using rays every (every frame) is often overkill. Consider using rays less frequently for example, every 2nd or 3rd frame) and interpolating the results.
If the environment against which you are broadcasting rays changes infrequently, cache the results of some rays. For example, if the player is standing still, you don’t need to re-broadcast the rays in the same direction every frame.
Definitely, prints destroy your optimization. 16 prints every frame is crazy, my PC would explode.
16 rays can be a lot. Think about whether you really need that much. Maybe 12 or 8 is enough.