Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is there a more efficient way to execute this code or no?

Asked by 6 years ago
game:GetService("StarterGui"):SetCore("ChatMakeSystemMessage",{
    Text = "Welcome to Ghost in the Graveyard!";
    Color = Color3.fromRGB(255,255,255);
    Font = Enum.Font.SourceSans;
    FontSize = Enum.FontSize.Size24;
})

wait(2)

game:GetService("StarterGui"):SetCore("ChatMakeSystemMessage",{
    Text = "Happy Halloween from TickTock Productions!";
    Color = Color3.fromRGB(255,165,0);
    Font = Enum.Font.SourceSans;
    FontSize = Enum.FontSize.Size24;
})

2 answers

Log in to vote
3
Answered by 6 years ago
Edited 6 years ago

You can make it more efficient by saving repeated expressions to local variables (in this case, local starterGui = game:GetService("StarterGui")). You can prevent the duplicate code by using a function:

local starterGui = game:GetService("StarterGui")
local defaultColor = Color3.fromRGB(255,255,255)
function SetMessage(text, color, font, fontSize) -- all arguments except 'text' are optional
    starterGui:SetCore("ChatMakeSystemMessage", {
        Text = text or error("Text is nil"),
        Color = color or defaultColor,
        Font = font or Enum.Font.SourceSans,
        FontSize = fontSize or Enum.FontSize.Size24,
    })
end
SetMessage("Welcome to Ghost in the Graveyard!")
wait(2)
SetMessage("Happy Halloween from TickTock Productions!")

(I put commas in the table, but your semicolons work too.)

[Edit]

You ask about how to make it a server script. To comply with FilteringEnabled, you have to break it up into two pieces: what the server will do and what the client will do. The client must always be responsible for the gui, but should never be responsible for keeping track of important information (like how much money someone has). The client also never has access to things like Datastores. Whatever the client cannot or should not do, the server must do instead.

If your intent is to make the server be able to specify new messages (ex game updates), you will need a RemoteEvent. The server should fire it whenever it wants the client to display a specific message and the client should listen for it and update the text.

0
Changing the color wouldnt work because if I say setMessage("Welcome!", 225,165,0) then it will think each number is a different parameter. What do I do then? TickTockTheory 106 — 6y
0
nvm figured it out TickTockTheory 106 — 6y
0
What happens if I want it to be a server script and not a local script? I tried pasting the code in a server script but nothing comes out TickTockTheory 106 — 6y
0
I updated the answer - you'll need to do additional reading/research if you don't know how to use RemoteEvents yet. chess123mate 5873 — 6y
Ad
Log in to vote
1
Answered by
xPolarium 1388 Moderation Voter
6 years ago
Edited 6 years ago

You can use a while loop with a wait parameter that fires that code every 'x' seconds and get a random message from a table.

local messageTable = {
    --If you're going to add more make sure they're seperated by a comma besides the last one

    "Welcome to Ghost in the Graveyard!",
    "Happy Halloween from TickTock Productions!"
}

local waitTime = 30; --Time we wanna wait after every message

while wait(waitTime) do
    local randomMessage = messageTable[math.random(1,#messageTable)]

    game:GetService("StarterGui"):SetCore("ChatMakeSystemMessage",{
        Text = randomMessage;
        Color = Color3.fromRGB(255,255,255);
        Font = Enum.Font.SourceSans;
        FontSize = Enum.FontSize.Size24;
    }) 
end

--I believe having the wait() inside the condition works better so it doesn't run at the beginning of the script.

Few other tricks but I think this is the best way.

0
I just want it to run at the beginning when someone joins TickTockTheory 106 — 6y

Answer this question