There’s cooldowns though, they aren’t infinite, so would this still have a great impact on player ping?
And since you said that, do you have a better way of making this?
There’s cooldowns though, they aren’t infinite, so would this still have a great impact on player ping?
And since you said that, do you have a better way of making this?
Not ping, by lag i mean fps and freezing games, it might affect the ping, not sure tho, just remove the while true loop, have the repeat until by itself
Would this freeze the game though?
Because the task.wait’s have throttling…
And whats the difference from having no while true loop but replacing it wilth a repeat?
The while true loop will run after it for no reason, you can check how much memory the script is taking, with this rate it would be a bit high
It isn’t, because its supposed to be a game loop.
So it basically goes back to intermission mode.
Oh alright then, still better to use a different type but your choice.
Thats why im asking…
Have a function which runs at different times
local times = {
10,
}
local funcs = {
intermission = function()
end,
}
while wait() do
for i , v in pairs(times) do
wait(v)
funcs[i]()
end
end
Forgot to add the check for players, you can have it in the functions table inside the intermission tho.
Why do you need to fire all clients anyway? Everything the server does will automatically replicate anyway.
I have a Remote Event that is supposed to change all of the players text, did you read the entire script?
Yes, but you don’t need a remote for that. Each client can figure that sort of thing out on their own. That’s not to say that you can’t use remotes for this, but there’s no real reason to. Basically over-complicating things.
Can you explain to me what I should use over this???
Clients can check how many players exist in the game. If you’re only using a special list of players, such as players that are prepared to enter a game, you can reference them using groups or teams. Or if you want a minimal change in code, you can just have a StringValue with the text you want displayed, and then have the client update their text when that StringValue is changed. Attributes work for both options as well.
No, I mean im using a remote event to Change a textLabel’s Text, Its not used to look for the players…
Please reread what I’ve written. The client scripts can automatically determine what the text label’s texts should be. I even gave an option with minimal changes to use a similar system without the use of remotes.
One more question, what is the difference in performance with having a repeat loop vs having a function that runs multiple times after another?
a repeat loop will just run over and over with a small delay and having multiple of them will greatly decrease fps especially if there is a lot of players, having a function which runs after a good amount of time is better since it won’t repeat itself over and over, it will wait until it’s the time for it to fire, like a full wait at once
local Game = game
local Players = Game:GetService("Players")
while #Players:GetPlayers() < 4 do --Wait for 4 players.
Players.PlayerAdded:Wait()
end
There are some misconceptions in here. Do not be afraid to use loops within your main game loop. Running a loop by itself is not costly and is actually quite neglible:
local players = game:GetService("Players")
task.wait(5)
-----------------Looping through 1 player --------------------
local timer = os.clock()
for i, player in pairs(players:GetPlayers()) do
end
print(os.clock() - timer) -->printed 0.0000039 seconds for me
task.wait(1)
-----------Looping 1000 times and adding 5 -----------
local counter = 0
timer = os.clock()
for i = 1, 1000 do
counter += 5
end
print(os.clock() - timer) -->printed 0.0000034 seconds for me
60 frames per second = 1/60 = ~.0167 seconds. You have ~.0167 seconds to execute any actions before the next frame is fired. This means that looping 1000 times and adding 5 can run ~4,912 times for me before it starts to lag! Granted this speed is based upon the device that is running the code, but if a device can’t even run that simple bit of code, then how is that device even able to run an operating system? So imagine what Roblox’s 32-core servers are capable of …
What needs to be considered when working with loops is how many times the loop runs and how extensive the code is within the loop:
local part = game.Workspace.Part
local timer = os.clock()
for i = 1, 100000 do
local newPart = part:Clone()
newPart.Name = "Part"..i
newPart.Position = Vector3.new(part.Position.X, part.Position.Y + (i * 5), part.Position.Z)
newPart.Parent = game.Workspace
end
print(os.clock() - timer) --> prints 1.118044 seconds, meaning it went wayyy over the .0167 allowance!
There are too many misconceptions on what causes “lag.” Everything causes lag when it is used too much…even too much of doing nothing can cause lag:
while true do
--crashes studio
end
this is actually running the loop so many times that studio crashes, but you get the point
I highly suggest using os.clock()
to test the speed of your code. Eventually it’ll become abundantly clear that most single operations are negligle:
--Loop within a loop (notice that another for loop is set up 1000 times!)
local timer = os.clock()
for i = 1, 1000 do
for ii = 1, 10 do
end
end
print(os.clock() - timer) --> prints .00002489 seconds
--Firing 1000 rays 1000 studs long in an empty workspace
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.IgnoreWater = true
local part = workspace.Part
local timer = os.clock()
for i = 1, 1000 do
local raycastResult = workspace:Raycast(part.Position, part.CFrame.LookVector*1000, raycastParams)
end
print(os.clock() - timer) --prints .001906 for 1000 rays 1000 studs long in an empty workspace
--Making 1000 tweens and parts
local timer = os.clock()
local TweenService = game:GetService("TweenService")
local part = game.Workspace.Part
local tweenTime = 5
local tableOfTweens = {}
local tweenInfo = TweenInfo.new(tweenTime)
local function CreateTween(clonedPart, differentPosition)
local goal = {}
goal.Position = Vector3.new(differentPosition, 10, 0)
goal.Color = Color3.new(0, 1, 0)
tableOfTweens[clonedPart.Name] = TweenService:Create(clonedPart, tweenInfo, goal)
end
for i = 1, 1000 do
local newPart = part:Clone()
newPart.Name = "Part"..i
newPart.Parent = game.Workspace.PartFolder
CreateTween(newPart, i-5)
end
print(os.clock() - timer) --> prints .013089 seconds to create BOTH 1000 tweens AND parts!
--Playing 1000 tweens at the same time:
task.wait(2)
local tweenFinishedCount = 0
timer = os.clock()
for partName, tween in pairs(tableOfTweens) do
tween:Play()
tween.Completed:Connect(function()
tweenFinishedCount += 1
if tweenFinishedCount == 1000 then
--NOTE: due to floating point imprecision I can't print out the math without sometimes having a negative number from the floating point errors!
--it isn't fair to say a tween "lagged" for 5 seconds when it is set to play for 5 seconds:
print("Subtract this number: ", os.clock() - timer, " from 5") --> .005873
--It also isn't fair to take its total processing time and compare it to the time of one frame, considering it took tweenTime * 60 frames to complete:
print("Take the above number and divide by this number: ", tweenTime * 60) --> 0.005873/300 = .00001957 second cost per frame for 1000 tweens!
end
end)
end
--Calling a function 1000 times which calls a module to get a value
local module = require(script.ModuleScript)
task.wait(5)
local function ReturnValueFromModule()
return module.GetValue()
end
local timer = os.clock()
for i = 1, 1000 do
local value = ReturnValueFromModule()
end
print(os.clock() - timer) --> prints .0000326 seconds
This is why many programmers caution not to over-optimize your code since doing so is usually pointless. If the slowest point of your code only ever reaches .00083 seconds, then optimizing that code will give you absolutely no real gain. Why waste your time over something that, in the end, will cause no difference at all? You will always get at least one person that experiences lag in your game even if your game is a blank baseplate. Focus on accomplishing the task and only revisit your code for optimization if there is a problem.