Can you save text in datastores?

Hello,

I am working on an announcement GUI. So you are able to announce things server wide. I have that whole system done and its working perfectly. I want to add another sections for Presets. This is so you don’t have to type the same things over and over again and you can announce or save them. I have the announcing part of it done but I am having troubles with the saving part? Maybe I am doing something wrong? Can you save text in datastores?

SERVER SCRIPT:

--// Datastore
local DatastoreService = game:GetService("DataStoreService")
local Save1 = DatastoreService:GetDataStore("Save1")
local Save2 = DatastoreService:GetDataStore("Save2") 
local Save3 = DatastoreService:GetDataStore("Save3")

--// SAVES AND SAVING
game.Players.PlayerAdded:Connect(function(Player)
	local SaveOne = Save1:GetAsync("UserId="..Player.UserId) or nil
	local SaveTwo = Save2:GetAsync("UserId="..Player.UserId) or nil
	local SaveThree = Save3:GetAsync("UserId="..Player.UserId) or nil
	Event2:FireClient(SaveOne, SaveTwo, SaveThree)
end)

Event2.OnServerEvent:Connect(function(Player, SlotToSave, WhatToSave)
	if SlotToSave == 1 then
		Save1:SetAsync("UserId="..Player.UserId, WhatToSave)
	elseif SlotToSave == 2 then
		Save2:SetAsync("UserId="..Player.UserId, WhatToSave)
	elseif SlotToSave == 3 then
		Save3:SetAsync("UserId="..Player.UserId, WhatToSave)
	else 
		print("Not a slot.")
	end
end)

EDIT: The error is on the server script, “Unable to cast value to Object” on “Event2:FireClient(SaveOne, SaveTwo, SaveThree)”

CLIENT SCRIPT:

--// Save Buttons
local SaveOne = script.Parent.Save1
local SaveTwo = script.Parent.Save2
local SaveThree = script.Parent.Save3

--// Text Inputs
local One = script.Parent.One.Message
local Two = script.Parent.Two.Message
local Three = script.Parent.Three.Message

local SaveEvent = game.ReplicatedStorage:WaitForChild("AnnouncementEvents"):WaitForChild("Save")

--// SAVES
local function Save(Slot)
	if Slot == 1 then
		SaveEvent:FireServer(Slot, One.Text)
	elseif Slot == 2 then
		SaveEvent:FireServer(Slot, Two.Text)
	elseif Slot == 3 then
		SaveEvent:FireServer(Slot, Three.Text)
	elseif Slot >= 4 then
		print("Not a slot")
	end
end

SaveEvent.OnClientEvent:Connect(function(LoadedOne, LoadedTwo, LoadedThree)
	One.Text = LoadedOne
	Two.Text = LoadedTwo
	Three.Text = LoadedThree
end)

SaveOne.Activated:Connect(function()
	Save(1)
end)

SaveTwo.Activated:Connect(function()
	Save(2)
end)

SaveThree.Activated:Connect(function()
	Save(3)
end)

Thanks for helping!

1 Like

Nothing wrong with saving a Text string to DataStores, it is simply that in the quote above you don’t pass any Player instance (client) to fire at. Instead, SaveOne is passed which obviously cannot be casted to a Player instance.

To fix this, legitimately just pass your Player:

Event2:FireClient(Player, SaveOne, SaveTwo, SaveThree);

FYI: you can easily shorten both your OnServerEcent callback and Save function by utilising an array.

What I mean is that you can slap them into one:

local SlotLabels = {One, Two, Three};

Then in your function, index your slot number and get the TextLabel needed. If there is nothing at that index, nil is (of course) returned.

Like so:

local function Save(Slot)
    local Label = SlotLabels[Slot];

    if (not Label) then 
        print("Not a slot!");
    else
        SaveEvent:FireServer(Slot, Label.Text);
    end
end

I would also suggest adding cooldowns to how fast the Player can save to avoid limits, though I anticipate that’s being worked on.

2 Likes

I feel so stupid! I should’ve know that lol! Thanks for the help!

1 Like