An in-game notification is a message that appears whenever a game wants you to pay attention to. One example of using in-game notifications is Jailbreak, whenever a player robs a store (Bank, Jewelery, Gas Station, etc.), it sends a notification to the police team. But how can I implement it into my own game?", you may ask. This tutorial shows you how to use in-game notifications in your game.
Creating a notification MUST be executed within a LocalScript, or else, the console would return this error and would not work:
StarterGui:SetCore must be called from a local script
To do this, you need to create a local script, preferably in StarterPlayer > StarterPlayerScripts, and enter this code:
game.StarterGui:SetCore("SendNotification", { -- Tell StarterGui to create a notification
Title = "Test",
Text = "Hello world!",
Duration = 5
})
Test this in Studio, and you should see a notification.
But what if we wanted to create a notification with buttons? Well, you can implement that in your notification! Now, enter this code to create a notification that has two buttons:
game.StarterGui:SetCore("SendNotification", { -- Tell StarterGui to create another notification, but with buttons
Title = "Message",
Text = "Why hello there!",
Button1 = "Hello",
Button2 = "Goodbye",
Duration = 5
})
Test this in Studio, and you should see a notification with two buttons.
In the code above, whenever either button is clicked on, it currently does nothing. This is because there is no code to handle when the button is clicked on.
Now, enter in this code:
local function callback(Text)
if Text == "Hello" then
print("user clicked on the Hello button")
else if Text == "Goodbye" then
print("user clicked on the Goodbye button")
end
end
end
local BindableFunction = Instance.new("BindableFunction")
BindableFunction.OnInvoke = callback -- When this is invoked, fire the function "callback"
game.StarterGui:SetCore("SendNotification", { -- Tell StarterGui to create a notification
Title = "Message",
Text = "Why hello there!",
Button1 = "Hello",
Button2 = "Goodbye",
Duration = 5,
Callback = BindableFunction
})
Before you can test this code, make sure you can view the Output in Studio. If not, click on View, then click on Output, and you should see the output.
Now, test the game, and, depending on what button you clicked on, you should see one of these messages in the output:
user clicked on the Hello button
user clicked on the Goodbye button
I hope that you learned someting new about notifications! If there are any issues in the code, feel free to send me a comment on this article or message me on the Roblox website.