I need help with making this script work!

I see, in that case you can update the onPlayerJoined event to load the saved text for the player and set it as the default text for the TextLabel. Here’s how you can do it:

local DEFAULT_TEXT = "Hello,\nworld!"
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local MyDataStore = DataStoreService:GetDataStore("MyDataStore")

local textLabel = script.Parent.TextLabel -- Change "TextLabel" to the name of your existing TextLabel

function loadText(player)
    local success, value = pcall(function()
        return MyDataStore:GetAsync(tostring(player.UserId))
    end)

    if success then
        return value or DEFAULT_TEXT
    else
        warn("Failed to load text for player " .. player.Name .. ": " .. tostring(value))
        return DEFAULT_TEXT
    end
end

function saveText(player, newText)
    local success, value = pcall(function()
        MyDataStore:SetAsync(tostring(player.UserId), newText)
    end)

    if not success then
        warn("Failed to save text for player " .. player.Name .. ": " .. tostring(value))
    end
end

function onPlayerJoined(player)
    local savedText = loadText(player)

    textLabel.Text = savedText

    player.Chatted:Connect(function(message)
        if string.sub(message, 1, 11) == "-changename" then
            local newName = string.sub(message, 13)
            player.Name = newName
        elseif string.sub(message, 1, 11) == "-changetext" then
            local newText = string.sub(message, 13)
            textLabel.Text = newText
            saveText(player, newText)
        end
    end)

    player.CharacterAdded:Connect(function()
        textLabel.Text = savedText
    end)

    player:LoadCharacter()
end

function onPlayerLeaving(player)
    saveText(player, textLabel.Text)
end

Players.PlayerAdded:Connect(onPlayerJoined)
Players.PlayerRemoving:Connect(onPlayerLeaving)

This updated onPlayerJoined event loads the saved text for the player using the loadText function, and then sets the TextLabel’s default text to the saved text using textLabel.Text = savedText. When the player rejoins, this should set the TextLabel’s text to the saved text from the DataStore instead of the default text.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.