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

How can I make this Event Receiver script more efficient?

Asked by
Memotag 226 Moderation Voter
6 years ago

I've made a script that consists solely of multiple Remote Event receivers (in the format listed below) which will change the text of a standard GUI it creates depending on which event occurs. As you can see the script is very clunky, especially considering that it is repeated 6 times for slightly different messages so I was wondering if there was any way I can make it more efficient while ensuring that every remote event it receives has it's own unique message and only 1 message appears at a time?

I really don't know where to start. All help is appreciated!

local plr = game.Players.LocalPlayer
local gui = game.ReplicatedStorage.MessageGui
local plrgui = plr.PlayerGui

-- Everything below is repeated 6 times in the same script

game.ReplicatedStorage.OwnsTycoonGui.OnClientEvent:Connect(function()
    if plrgui:FindFirstChild("MessageGui") ~= nil and plrgui:FindFirstChild("MessageGui").Message.Text ~= "1" then -- 1 = message number
        print(plr.Name)
        plr.PlayerGui.MessageGui.Message.Text = "1"
        plr.PlayerGui.MessageGui.Message.Alert.Text = "Message"
    end
    if plrgui:FindFirstChild("MessageGui") == nil then
        gui:Clone().Parent = plr.PlayerGui
        plr.PlayerGui.MessageGui.Message.Text = "1"
        plr.PlayerGui.MessageGui.Message.Alert.Text = "Same message as above"
    end
    wait(3)
    if plrgui:FindFirstChild("MessageGui") ~= nil and plrgui:FindFirstChild("MessageGui").Message.Text == "1" then
        plr.PlayerGui.MessageGui:Destroy()
    end
end)
0
By not using ~= nil or == nil http://robloxdev.com/articles/Writing-Clean-Code User#19524 175 — 6y

1 answer

Log in to vote
-3
Answered by 6 years ago

Use

if not plrgui:FindFirstChild("MessageGui")

rather than

if plrgui:FindFirstChild("MessageGui") ~= nil and
0
Isn't fixing the problem, but I would use the former over the latter. User#19524 175 — 6y
Ad

Answer this question