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)
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
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 ;) ]