Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can i use DataStore to save a Gui?

I am making a gui shop where you can buy weapons and gears with a currency called gems. When i want to buy a weapon and i press a "Buy" button, that button changes to an "Equip" button. My problem is i want to know how i can add a datastore to my script so that when i leave the game, the button still appears as an equip button only for me.

This is my line of code as a Local Script inside the buy button

local player = game.Players.LocalPlayer
local gems = player:WaitForChild('leaderstats').Gems
local buyButton = script.Parent
local equipButton = script.Parent.Equip
local errorMessage = script.Parent.Parent.Parent.Parent.Parent.ErrorMessage
local gemImage = script.Parent.Gem

script.Parent.MouseButton1Click:connect(function()
	if gems.Value >= 50 and not player.Backpack:FindFirstChild('HyperlaserGun') and not player.StarterGear:FindFirstChild('HyperlaserGun') and not 
player.Character:FindFirstChild('HyperlaserGun') then
	game.Lighting.WeaponFolder.HyperlaserGun:Clone().Parent = player.Backpack
	game.Lighting.WeaponFolder.HyperlaserGun:Clone().Parent = player.StarterGear
		gems.Value = gems.Value - 50
		equipButton.Visible = true
		gemImage.Visible = false
	else
		if player.Backpack:FindFirstChild('HyperlaserGun') or player.StarterGear:FindFirstChild('HyperlaserGun') or player.Character:FindFirstChild('HyperlaserGun') then
			errorMessage.Visible = true
			errorMessage.Text = 'You already own this item!'
			wait(1.5)
			errorMessage.Visible = false
			errorMessage.Text = "You don't have enough Gems to purchase this!"
		else	
		errorMessage.Visible = true
		wait(1.5)
		errorMessage.Visible = false			
			
		end
	end
end)

2 comments

1

you save the "Stats" not the "Gui"

0

Yes, you would save values when using data store. There is no such thing is "saving GUIs" because GUIs use values to present themselves.

2 answers

✓ Accepted answer

You could try something like this. To set it to true use the same method and just replace the value in the string, then set. Not tested.

-- Use this as a format: local tableExample = "item1=false;item2=true;item3=false;"

function ReturnAll(dbname, playerid)
	local tableExample = game:GetService("DataStoreService"):GetDataStore(dbname):GetAsync(playerid);
	local spotsc = {};
	local spotss = {};
	local formc = 1;
	local forms = 1;
	local ret = {};
	for i = 1, #tableExample do
		if tableExample:sub(i,i) == ";" then
			spotsc[formc] = i;
			formc = formc + 1;
		elseif tableExample:sub(i,i) == "=" then
			spotss[forms] = i;
			forms = forms + 1;
		end
	end
	
	for i=1, formc - 1 do
		local eq = spotss[i]
		local ec = spotsc[i]
		local name;
		local value;
		if i == 1 then
			name = tableExample:sub(1,eq-1);
			value = tableExample:sub(eq+1,ec-1);
		else
			name = tableExample:sub(spotsc[i-1]+1,eq-1);
			value = tableExample:sub(eq+1,ec-1);
		end
		
		if value == "false" then
			ret[name] = false;
		else
			ret[name] = true;
		end
	end
	return ret;
end

if ReturnAll("testdb", 1)["someitem"] == true then
	-- Returns true.
end

2 comments

0

Thanks it helped!

0

What would I put this script into?

Booleans

You can have a Boolean for each item True == owns item false == doesnt own item

So if DS1 = Datastore:GetDataStore("SomeItemYouHaveInYourGame")

PlayerHasItem = DS1:GetAsync(player.UserId)

If PlayerHasItem == True then
	Print("Player owns item")
end

idk my lua code is pretty stupid because this is off the top of my head but i hope this was what you were looking for. ~ AnanonymousDeveloper

[problem with this method is youll have to make a datastore for every individual store item. unless your smarter than me and can figure out another way of doing it ;) ]

3 comments

0

do i put that in the same script or a different one?

0

Basically you want Separate server scripts to have your data stores in. So once you made the data stores then you can save the booleans.

0

I wouldnt recommend putting it into that script you previewed

This discussion is preserved as it was. New answers, comments, and votes are closed.