I am tryin to make a laser attachment script and i’m currently having trouble making the laser raycast smooth. I tried using wait() but it seems to be choppy. How do I use renderstepped in place of wait()? Also does having RenderStepped loops lag the game?
function lazr(active)
if active then
connection = rs.RenderStepped:Connect(function()
local Beam = Instance.new("Part", game.Workspace.ignore)
Beam.Anchored = true
Beam.CanCollide = false
Beam.Color = script.Colour.Value
Beam.Material = Enum.Material.Neon
Beam.Name = "LaserBeam"
local Raycast = Ray.new(script.Parent.CFrame.p, script.Parent.CFrame.LookVector * 500)
local Part, Position = game.Workspace:FindPartOnRayWithIgnoreList(Raycast, {script.Parent, Beam}, false, true)
local Distance = (script.Parent.CFrame.p - Position).Magnitude
Beam.Size = Vector3.new(0.05, 0.05, Distance)
Beam.CFrame = CFrame.new(script.Parent.CFrame.p, Position) * CFrame.new(0, 0, -Distance/2)
wait() -- need to be replaced
Beam:Destroy()
end)
else
if connection then connection:Disconnect()
end
end
end
Server:
[Heartbeat only]
RS.Heartbeat:Wait() and RS.Heartbeat:Connect(function()
Client:
[RenderStepped and Heartbeat]
RS.RenderStepped:Wait() and RS.RenderStepped:Connect(function()
RS.Heartbeat:Wait() and RS.Heartbeat:Connect(function()
The client model, RenderStepped, runs faster and is recommended for camera and character code. You can use RenderStepped for raycasting, but it may lead to performance throttling.
I recommend doing raycasting on the client, this will reduce server delay and improve client accuracy. I also recommend you use workspace:Raycast() instead of Ray.new(), it is a more recent raycasting method and doesn’t require a ray object.
local RS = game:GetService("RunService")
RS.Heartbeat:Connect(function()
-- Code here runs every frame following internal physics simulation.
end)
local RS = game:GetService("RunService")
RS.RenderStepped:Connect(function()
-- Code here runs before every frame.
-- Frame doesn't load until after RS finishes. (Throttles internal processes!)
end)