Is the way i’m scripting now in is a good way or not?
What should i change if not?
local MinPlayers = 3
local minute = 60
local Time = minute * 3 + 20
local NumberOfDisasters = 4
local MessageService = game.Workspace.Game.Functions:WaitForChild("Message")
local GenerateMap = game.Workspace.Game.Functions:WaitForChild("GenerateMap")
local DisasterService = game.Workspace.Game.Functions:WaitForChild("Disaster")
local TeleportService = game.Workspace.Game.Functions:WaitForChild("TeleportPlayers")
local WinService = game.Workspace.Game.Functions:WaitForChild("IsWinFunction")
----
----high level functions
----
function message (msg)
MessageService:Invoke(msg)
end
----
----Low level functions
----
function disaster()
local disasters = DisasterService:Invoke(true)
while disasters == nil do
wait()
end
local Cat = disasters[math.random(#disasters)]
DisasterService:Invoke(Cat)
end
function Countdown(t)
for n = t,0,-1 do
message("Seconds Left:"..n)
end
end
function GetNumOfPlayers()
return #game.Players:GetChildren()
end
function RunGame ()
for n = 1,NumberOfDisasters do
disaster()
wait(Time/NumberOfDisasters)
end
end
function RunLowLevelFunctions()
spawn(function()
RunGame()
end)
end
function intermission ()
local t = 30
for n = t,0,-1 do
message("Intermission:"..n)
wait(1)
end
end
function give (plrs)
for i,v in pairs(plrs) do
local leaderstatsUI = v.PlayerGui:FindFirstChild("StatsUI")
if leaderstatsUI then
local folder = leaderstatsUI.leaderstats
if folder then
local atr = folder:GetAttributes()
local coins = atr.Coins
local XP = atr.XP
XP += math.random(50,100)
coins += math.random(5,10)
end
end
end
end
-----
-----Loop
-----
while wait() do
local plrs = 0
if plrs < MinPlayers then
message("Waiting for more players")
while wait() and plrs < MinPlayers do
plrs = 0
plrs = GetNumOfPlayers()
print(plrs)
wait(0.1)
end
end
intermission()
message("Generating map")
local cords = GenerateMap:Invoke()
message("Teleporting players")
TeleportService:Invoke(cords[1],cords[2],cords[3])
RunLowLevelFunctions()
WinService:Invoke("CREATE")--method CREATE (creates list of players)
Countdown(Time)
local winners = WinService:Invoke("GET")--method GET returns list of player that survived
give(winners)
end
This actually looks quite basic (I’m not saying its easy, you’ve just made it look simple) and a very nice layout for something that I thought would be advanced.
You’ve used a very nice order and sorting of functions.
function GetNumOfPlayers()
return #game.Players:GetChildren()
end
This I personally wouldn’t use but I guess it saves some time.
If you have some experience with coding this would be very readable but if you can understand it well its fine. The use of functions make the actual ‘bulk’ code very short and easy to understand
Are you sure you meant #game:GetService("Players"):GetPlayers()? It’s a quick way to getting the number within table of players without having issues of unexpected objects in Players. The function is pretty much redundant. Canonically, you would use the one that I have mentioned and save that reference to a variable.
There’s a few things I would mention about your code
I recommend you make a variable for game.Workspace.Game.Functions as you use it quite a few times, and I would also recommend to use workspace instead of game.Workspace as they’re the same thing but one is shorter and is a direct reference to the Workspace service instead of indexing
I’m not sure why you have functions that are just 1 line, such as your message and GetNumOfPlayers functions, functions typically are used to prevent repetition of many lines, not just a single one, but I guess it’s fine if the name of the function is more helpful for you
Use game:GetService("Players"):GetPlayers() rather than game.Players:GetChildren() as it ensures you only get players
Get services using game:GetService(Service) instead of game.Service as it ensures as well that you get the service and not something else, they both do the same but one is safer in the event you accidentally rename a service for example
I’m not understanding why you did
plrs = 0
plrs = GetNumOfPlayers()
In the loop that waits for more players to join, did you mean to only do the 2nd line? Also, wouldn’t be better that in this line
local plrs = 0
You do
local plrs = GetNumOfPlayers()
So it wont do that message even if there’s the correct amount of players
A bit of a slight nitpick but you only use the minutes variable in another place once, I think it would be better to use 60 in that case for your Time variable, you should only really make a variable for something if you use it more than once personally
Another nitpick but I’d recommend localizing your functions for a bit of added consistency with your variables, that and it has became commonplace to localize functions and variables
Last thing to mention as a recommendation, you really should be the v in your give function a more descriptive name such as player to prevent that small bit of confusion for you and readers, and to change the i to a _ to show you and the reader that it is an unused/dummy argument, sine I don’t see it being used there
That’s all I really have to say, besides that, everything else looks good!