How to create a BillboardGUI that is saved in datastore?

Hi, I have created a BillboardGUI and put it in the player’s head (all from server side) and I would like to save it in datastore, I want it to be saved.

Is it okay what I have done?
I’m a bit worried, as I would like each “Role” (different names) to have their own custom BillboardGUI on their head.

For example, if it is Role “player” then the billboardgui will say “New player” things like that.

I would also like to know how to save in datastore, should I follow the same steps as with the stringValue? I would very much appreciate a hand, thank you! :smiley:

Server script:

local DataStoreService = game:GetService('DataStoreService')
local RanksDataStores = DataStoreService:GetDataStore('RanksDataStores')

local BillboardDataStores = DataStoreService:GetDataStore('BillboardDataStores')

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new('Folder', player)
	leaderstats.Name = 'leaderstats' 

	local Ranks = Instance.new('StringValue', leaderstats)
	Ranks.Name = 'Roles'
	
	-- NEW! billboard here.
	local BillboardGUI = Instance.new('BillboardGui', player.Character.Head)
	BillboardGUI.Name = 'Billboard'
	
	BillboardGUI.Size = UDim2.new(3, 0, 1, 0)
	local TextLabel = Instance.new("TextLabel", BillboardGUI)
	TextLabel.Text = "New Player!"
	TextLabel.Size = UDim2.new(1,0,1,0)
	print("BillboardGUI Given to "..player.Name)
	
	-- BillboardData
	local BillboardData
	local bsuccess, berrormessage = pcall(function()
		BillboardData = BillboardDataStores:GetAsync('BillboardGUI_'..player.UserId)
	end)
	
	if BillboardData and bsuccess then
		player.Character.Head.BillboardGUI.TextLabel.Text = "New Text"
		print("Billboard Data saved!")
	else
		warn(berrormessage)
		print("In the billboard data is something wrong.")
	end
	
	local RanksData

	local success, errormessage = pcall(function()
		RanksData = RanksDataStores:GetAsync('Ranks_'..player.UserId)
	end)

	if RanksData and success then
		player.leaderstats.Roles.Value = RanksData
		print("Data has been saved!")
	else
		warn(errormessage)
		print("There is something wrong.")
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local Ranks = player.leaderstats.Roles
	local success, errormessage = pcall(function()
		RanksDataStores:SetAsync("Ranks_" .. player.UserId, Ranks.Value)
	end)
	
	local Billboard = player.Character.Head.BillboardGUI.TextLabel
	local bsuccess, berrormessage = pcall(function()
		BillboardDataStores:SetAsync("Billboard_".. player.UserId, Billboard.Text)
	end)
end)

and i got an error:

image

3 Likes

Can you show me the line please?

sure :smiley:

image

Ok make a character variable like this:

local Character = player.Character or player.CharacterAdded:Wait() -- try this
1 Like

By the way because it is in the characters head it will not show on it you need to set its adornee to the characters head aswell lol (Just so you know)

1 Like

Thanks! This error was fixed!
but does this billboardgui text, will it be saved in datastore?

I need the text value to be saved in datastore in order for the players to keep the text that is assigned to them

image

Data shows there’s something wrong :c how could i save the billboard text?

image

So then I do not know I never saved GUIs before but try this:

--Instead of:
player.Character.Head.BillboardGUI.TextLabel.Text = "New Text"

--do:
player.Character.Head.BillboardGUI.TextLabel.Text = RanksData

I am sorry I am trying I never ever saved GUIs before if I cannot help I am sorry :frowning:

It didn’t work, thanks anyway. :smiley:
I’m not sure how I should datastore the text above the player’s head :scream:

I do not know either try going on youtube.com

1 Like

Sorry I couldn’t help :frowning:

1 Like

This line is problematic.

BillboardData = BillboardDataStores:GetAsync('BillboardGUI_'..player.UserId)

The keys are totally different from each other, they are not saved on "BillboardGUI_UserId", but Billboard_UserId. See below.

BillboardDataStores:SetAsync("Billboard_".. player.UserId, Billboard.Text)

I’ve already fixed that bug, but I still don’t know how to make the billboardgui text save to datastores…

Updated script

local DataStoreService = game:GetService('DataStoreService')
local RanksDataStores = DataStoreService:GetDataStore('RanksDataStores')

local BillboardDataStores = DataStoreService:GetDataStore('BillboardDataStores')

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new('Folder', player)
	leaderstats.Name = 'leaderstats' 

	local Ranks = Instance.new('StringValue', leaderstats)
	Ranks.Name = 'Roles'
	
	-- NEW! billboard here.
	local Character = player.Character or player.CharacterAdded:Wait()
	
	local BillboardGUI = Instance.new('BillboardGui', Character.Head)
	BillboardGUI.Name = 'Billboard'
	
	BillboardGUI.Size = UDim2.new(3, 0, 1, 0)
	local TextLabel = Instance.new("TextLabel", BillboardGUI)
	TextLabel.Text = "New Player!"
	TextLabel.Size = UDim2.new(1,0,1,0)
	print("BillboardGUI Given to "..player.Name)
	
	-- BillboardData
	local BillboardData
	local bsuccess, berrormessage = pcall(function()
		BillboardData = BillboardDataStores:GetAsync('BillboardGUI_'..player.UserId)
	end)
	
	if BillboardData and bsuccess then
		player.Character.Head.BillboardGUI.TextLabel.Text = BillboardData -- if doesnt work then try tostring(RanksData)
		print("Billboard Data saved!")
	else
		warn(berrormessage)
		print("In the billboard data is something wrong.")
	end
	
	local RanksData

	local success, errormessage = pcall(function()
		RanksData = RanksDataStores:GetAsync('Ranks_'..player.UserId)
	end)

	if RanksData and success then
		player.leaderstats.Roles.Value = RanksData
		print("Data has been saved!")
	else
		warn(errormessage)
		print("There is something wrong.")
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local Ranks = player.leaderstats.Roles
	local success, errormessage = pcall(function()
		RanksDataStores:SetAsync("Ranks_" .. player.UserId, Ranks.Value)
	end)
	
	local Character = player.Character or player.CharacterAdded:Wait()
	local bsuccess, berrormessage = pcall(function()
		BillboardDataStores:SetAsync("Billboard_".. player.UserId, Character.Head)
	end)
end)

You still aren’t saving this correctly. You’re trying to save the Character.Head instance in that script. I believe you should try finding the BillboardGui and then the label object.

1 Like

How should I do it to make it work? :scream:

Does this do what you need it to do?

local DS = game:GetService("DataStoreService")
local Datastore = DS:GetDataStore("MyDatastore")

game.Players.PlayerAdded:Connect(function(player)
	local Char = player.Character or player.CharacterAdded:Wait()
	local leaderstats = Instance.new('Folder')
	leaderstats.Name = 'leaderstats'
	leaderstats.Parent = player

	local Ranks = Instance.new('StringValue')
	Ranks.Name = 'Roles'
	Ranks.Parent = leaderstats

	-- NEW! billboard here.
	local BillboardGUI = Instance.new('BillboardGui')
	BillboardGUI.Name = 'Billboard'
	BillboardGUI.Size = UDim2.new(3, 0, 1, 0)
	BillboardGUI.Parent = Char.Head

	local TextLabel = Instance.new("TextLabel")
	TextLabel.Size = UDim2.new(1,0,1,0)
	TextLabel.Name = "TextLabel"
	TextLabel.Parent = BillboardGUI

	print("BillboardGUI Given to "..player.Name)

	-- BillboardData
	local Data

	local success, errormessage = pcall(function()
		Data = Datastore:GetAsync(player.UserId)
	end)

	if success then
		print("Loaded "..player.Name)
		if Data ~= nil then
			print(player.Name.." has saved data")
			TextLabel.Text = Data.Billboardtext
			Ranks.Value = Data.Ranks
		else -- If the Player is new
			print(player.Name.." doesn't have saved data")
			TextLabel.Text = "NewText" -- Or put what you want if the player is new
			Ranks.Value = "" -- Or put what you want if the player is new
		end
	else
		warn(errormessage)
		print("In the data is something wrong.")
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local Data = {
		Rank = player.leaderstats.Roles.Value;
		BillboardText = player.Character.Billboard.TextLabel.Text
	}
	local success, errormessage = pcall(function()
		Datastore:SetAsync(player.UserId, Data)
	end)
	if success then
		print("Saved "..player.Name)
	else
		print("Error saving "..player.Name)
	end
end)
1 Like

Yes, that exactly! thanks a lot! By the way, how can I make the billboard a little higher? and how can i do to keep its size (text) along with the billboard when i do zoom-in and zoom-out in the game?? :scream:

You can change the BillboardGui’s StudsOffset value to make it higher. You can turn on the TextLabel’s TextScaled value to make the text scale with the gui.

I have added this to the script:

local DS = game:GetService("DataStoreService")
local Datastore = DS:GetDataStore("MyDatastore")

game.Players.PlayerAdded:Connect(function(player)
	local Char = player.Character or player.CharacterAdded:Wait()
	local leaderstats = Instance.new('Folder')
	leaderstats.Name = 'leaderstats'
	leaderstats.Parent = player

	local Ranks = Instance.new('StringValue')
	Ranks.Name = 'Roles'
	Ranks.Parent = leaderstats

	-- NEW! billboard here.
	local BillboardGUI = Instance.new('BillboardGui')
	BillboardGUI.Name = 'Billboard'
	BillboardGUI.Size = UDim2.new(3, 0, 1, 0)
	BillboardGUI.StudsOffset = Vector3.new(0, 2, 0) -- change the number 2 to make it go higher or lower
	BillboardGUI.Parent = Char.Head

	local TextLabel = Instance.new("TextLabel")
	TextLabel.Size = UDim2.new(1,0,1,0)
	TextLabel.Name = "TextLabel"
	TextLabel.TextScaled = true
	TextLabel.Parent = BillboardGUI

	print("BillboardGUI Given to "..player.Name)

	-- BillboardData
	local Data

	local success, errormessage = pcall(function()
		Data = Datastore:GetAsync(player.UserId)
	end)

	if success then
		print("Loaded "..player.Name)
		if Data ~= nil then
			print(player.Name.." has saved data")
			TextLabel.Text = Data.Billboardtext
			Ranks.Value = Data.Ranks
		else -- If the Player is new
			print(player.Name.." doesn't have saved data")
			TextLabel.Text = "NewText" -- Or put what you want if the player is new
			Ranks.Value = "" -- Or put what you want if the player is new
		end
	else
		warn(errormessage)
		print("In the data is something wrong.")
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local Data = {
		Rank = player.leaderstats.Roles.Value;
		BillboardText = player.Character.Billboard.TextLabel.Text
	}
	local success, errormessage = pcall(function()
		Datastore:SetAsync(player.UserId, Data)
	end)
	if success then
		print("Saved "..player.Name)
	else
		print("Error saving "..player.Name)
	end
end)
1 Like

Thanks you a lot brother! :smiley: I really appreciate it, you have helped me a lot! :smiley:

1 Like