Best way to make player joined message and animation match after choosing a random message?

I want the player to get a random join message that matches the randomly chosen join animation. For example, if a player joins and lightning strikes where they spawn, I would want it to say “(Username) has come to strike you down” or something like that.

I’m guessing the best way to do this would basically be like what I’ve written below. Sorry it’s not actually code, I’m new to this and didn’t wanna write something completely wrong.

When the player joins, pick a random number 1-10

if the number is 1, send message "(Username) has come to strike you down"
play animation 1

else
if the number is 2, send message "(Username), destroyer of worlds, had arrived"
play animation 2

end

Is this what I should do, or is there a better way? Instead of playing the animation directly, should I call on another script to play it so that I can add and remove parts that are used in the animation like the lightning?

Edit: For clarification, I mean a message in the chat.

I’d use a table to store each string and animation and play whatever the table has for the index generated from the number. As an example, here’s how I’d print something in the output window (ie. not the chat window):

local index = math.random(1, 4)

local messages = {
    "Hello world!",
    "John Doe",
    "foobar"
}

print(messages[index]) -- Prints a random string from the table

This concept can also be applied to animations. You can store it in a nested table combined with the message, or store it in a separate table and make sure the animations have matching indices with their corresponding message.

As for whether or not you should play the animation in a separate script, it doesn’t sound like a detrimental idea to me. Do it if you think it will help with maintainability down the line. After all, the only bad thing that will come of it is that you’ll have 2 scripts to organize.

1 Like

I would personally implement the animations in a separate module. That way you can have a fine line between join logic and animation logic. You can combine the messages and animations functions in a table:

local JOIN_EVENTS = {
    { Message = "A", Animation = Animations.A },
    { Message = "B", Animation = Animations.B },
    { Message = "C", Animation = Animations.C },
}

When the player joins the game, you’ll select a random join event

local function runRandomJoinEvent(player: Player)
    local randomJoinEvent = JOIN_EVENTS[math.random(#JOIN_EVENTS)]

    -- Display randomJoinEvent.Message
    -- Run randomJoinEvent.Animation
end
1 Like

If you mean system messages, then its a bit of a hassle.
Because roblox made it so to display system messages it has to be on the client.
So, You could do something like this:

local Players = game:GetService("Players")
local messageAnimationList = {
  [1] = {
         message = "%s has come to strike you down", 
         animation = 000000 --anim id 1
        },
  [2] = {
         message = "%s, destroyer of worlds, had arrived", 
         animation = 000000 -- anim id 2
        } -- You could always add more here
}

Players.PlayerAdded:Connect(function(player)
   local randomIndex = math.random(1, #messageAnimationList)
   local selected = messageAnimationList[randomIndex]
   -- btw the %s is for formatting the placeholder position for player's Name
   local formattedMessage = string.format(selected.message, player.Name)

   game.ReplicatedStorage.JoinRemote:FireAllClients(formattedMessage)

   local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
    if humanoid then
        local animation = Instance.new("Animation")
        animation.AnimationId = "rbxassetid://"..selected.animation
        local animator = humanoid:FindFirstChildOfClass("Animator") or humanoid:WaitForChild("Animator")
        local animationTrack = animator:LoadAnimation(animation)
        animationTrack:Play()
    end
end)

Then on a client script, to replicate the join message to everyone:

local TextChatService = game:GetService("TextChatService")
local JoinRemote = game:GetService("ReplicatedStorage"):WaitForChild("JoinRemote")

JoinRemote.OnClientEvent:Connect(function(message)
   TextChatService.TextChannels.RBXSystem:DisplaySystemMessage(message)
end)
1 Like

Thank you! I meant a chat message but this still helps a lot!

1 Like