Currently, I have a round system set to a function, I have a loop that checks if a variable is true, but wouldn’t this rapid call the function causing the game to eventually lag?
What should I do?
Server sided btw
game.Players.PlayerAdded:Connect(function(player)
if table.find(Permitted, player.UserId) then
player.Chatted:Connect(function(msg)
if msg == "!EyeEvent" then
if game.Workspace.HappyHome then
LaserPart.Parent = workspace
MinigameActive = true
else
Map:Clone().Parent = workspace
LaserPart.Parent = workspace
MinigameActive = true
end
elseif msg == "!EndEvent" then
MinigameActive = false
wait(2)
LaserPart.Parent = game.ReplicatedStorage
elseif msg == "!Reset" then
workspace.HappyHome:Destroy()
Map:Clone().Parent = workspace
end
end)
end
end)
MinigameActive = false
local target_previous = Vector3.new()
function startGame(Target)
local origin = rayOriginPart.CFrame.Position
local rayTarget = target_previous:Lerp(Target.Position, laser_TrackingAccuracy)
local direction = (rayTarget - origin)
local results = workspace:Raycast(origin, direction * 100, raycastParameters)
if not results then
results = {Position = rayTarget}
else
print(results.Instance)
end
local distance = (results.Position - origin).Magnitude
local LaserPosition = laserPart.Position
LaserPart.Size = Vector3.new(laser_Thickness,laser_Thickness,distance)
laserPart.CFrame = CFrame.new(origin, results.Position) * CFrame.new(0,0, -distance/2)
target_previous = rayTarget
end
while true do
if MinigameActive == true then
Runservice.Stepped:Connect(function(dt)
local target = FindNearest(rayOriginPart.Position)
if target then
startGame(target.HumanoidRootPart)
end
end)
end
wait(10)
end