EDIT: I fixed this by using OrderedDataStore. Do not answer

Ok so prepare yourself. This is a hard one!

So I am trying to make a shop that can give you trails if you give coins. I have gotten pretty far and it works great so far.

Things that work:

  • Equipping and unequipping. Works amazing!
  • Buying system. Everything purchases fantastically
  • Organizing using UILayouts. Looks amazing

Things that don't:

  • Saving system. I have to rebuy the trails upon leaving.

Everything works like a charm. There are no errors and it works fine except for saving and loading.

The saving system flat out doesn't work. I am using HTTPService to turn the data into strings to post to the stores. It just won't work! Can someone tell me why? Thanks to any who do. This is a hard question.

Hierarchy of GUI: https://i.gyazo.com/a49d083e9d0513e1d1b7e3842af568e0.png

Script:

local OpenButton = script.Parent.OpenClose.TextButton
local Template = script.Item
local Frame = script.Parent.Frame
local Player = script.Parent.Parent.Parent
local DS = game:GetService("DataStoreService")
local OwnedStore = DS:GetDataStore("Owned")
local EquippedStore = DS:GetDataStore("Equipped")
local HTTPService = game:GetService("HttpService")

local Trail = game.ReplicatedStorage.Trail
local Equipped
local Owned = {}

local OwnedData
local EquippedData

local success, fail = pcall(function()
	EquippedData = EquippedStore:GetAsync(Player.UserId)
	OwnedData = OwnedStore:GetAsync(Player.UserId)
end)

if success then
	print("Sucess")
	
	if Equipped == nil then
		Equipped = HTTPService:JSONDecode(EquippedData)
	else
		Equipped = HTTPService:JSONDecode(EquippedData)
	end
	Owned = HTTPService:JSONDecode(OwnedData)
	print(tostring(Owned))
	print(tostring(Equipped))
else
	warn(fail)
end

local Colors = {BrickColor.new("Really red"), BrickColor.new("Bright blue"),BrickColor.new("Bright green"), BrickColor.new("Brick yellow")}
local Names = {"Red trail","Blue trail","Green trail","Yellow trail"}
local Prices = {50,100,150,200}

function Update()
	
		
		
	for i,item in pairs(Frame.Shop:GetChildren()) do
		if item.Name == "Item" then
			--Is an item
			print("Item")
			local Color = BrickColor.new(item.ImageColor3)
			if isInTable(Owned,Color) == true then
				print("Owned")
				if Equipped == Color then
					item.Button.TextButton.Text = "Equipped"
				else
					item.Button.TextButton.Text = "Equip"
				end
			else
				--Nothing
				print("Not owned")
			end
		end
	end
	local Fire = Player.Character.Torso:FindFirstChild("Trail")
	if Fire then
		if Equipped == nil then
			Fire:Destroy()
		else
			Fire.Color = Equipped.Color
		end
	else
		if Equipped == nil then
			
		else
			local Fire = Trail:Clone()
			Fire.Parent = Player.Character.Torso
			Fire.Color = Equipped.Color
		end
			
	end
end
Update()
function isInTable(tableValue, toFind)
	local found = false
	for _,v in pairs(tableValue) do
		if v==toFind then
			found = true
            break;
		end
	end
	return found
end

OpenButton.MouseButton1Click:Connect(function()
	if Frame.Visible == false then
		Frame.Visible = true
	else
		Frame.Visible = false
	end
end)




function Equip(Color,Player)
	local Fire = Player.Character.Torso:FindFirstChild("Trail")
	if Equipped == nil then
		
	else
		Update()
	end
	if Fire then
		--Player already has trail
		Fire.Color = Color.Color
	end
	Equipped = Color
	Update()
end


for i, color in pairs(Colors) do
	local Clone = Template:Clone()
	Clone.Parent = Frame.Shop
	Clone.TextLabel.Text = Names[i]
	Clone.Button.TextButton.Text = "Buy item - "..Prices[i].." coins"
	Clone.ImageColor3 = color.Color
	
	
	
	Clone.Button.TextButton.MouseButton1Click:Connect(function()
		if isInTable(Owned,color) == true then
			--Nothing
			if Equipped == color then
				Equipped = nil
				local Fire = Player.Character.Torso:FindFirstChild("Trail")
				if Fire then
					Fire:Destroy()
				end
				Clone.Button.TextButton.Text = "Equip"
			else
				Clone.Button.TextButton.Text = "Equipped"
				Equip(color,Player)
			end
			Update()
		else
			local Coins = Player.leaderstats.Coins
			if Coins.Value >= Prices[i] then
				print("Bought "..Clone.TextLabel.Text)
				Coins.Value = Coins.Value - Prices[i]
				table.insert(Owned,color)
				Update()
				if Equipped ~= nil then
					EquippedStore:SetAsync(Player.UserId,HTTPService:JSONEncode(Equipped))
				end
				OwnedStore:SetAsync(Player.UserId,HTTPService:JSONEncode(Owned))
			else
				Clone.Button.TextButton.Text = "Not enough coins!"
				wait(1)
				Clone.Button.TextButton.Text = "Buy item - "..Prices[i].." coins"
			end
		end
		Update()
	end)
	
end
function SaveStores()
	if Equipped ~= nil then
			EquippedStore:SetAsync(Player.UserId,HTTPService:JSONEncode(Equipped))
		end
	OwnedStore:SetAsync(Player.UserId,HTTPService:JSONEncode(Owned))

end
spawn(SaveStores)
while true do
	wait(1)
	Update()
end
game.Players.PlayerRemoving:Connect(function(plr)
	if plr.Name == Player.Name then
		print("Correct player")
		print(tostring(Owned))
		print(tostring(Equipped))
		if Equipped ~= nil then
			EquippedStore:SetAsync(plr.UserId,HTTPService:JSONEncode(Equipped))
		end
		OwnedStore:SetAsync(plr.UserId,HTTPService:JSONEncode(Owned))
	else
		print("no")
	end
end)

2 comments

0

Hey, i would recommend using datastore2

0

Add [SOLVED] at the beginning of your title, so that it gets marked as solved.