Datastore help (Shop Gui)

Im trying to make a one-time purchase shop gui

ive made it here and its not working pls help

Shop Handler of server
local dss = game:GetService("DataStoreService")
local ds = dss:GetDataStore("ToolShop")

game.Players.PlayerAdded:Connect(function(player)
	
	local ToolFolder = Instance.new("Folder",player)
	ToolFolder.Name = "ToolFolder"
	
	local hasSword = Instance.new("BoolValue",ToolFolder)
	hasSword.Value = false
	hasSword.Name = "hasSword"
	
	local OwnsSword = Instance.new("BoolValue",ToolFolder)
	OwnsSword.Value = false
	OwnsSword.Name = "OwnsSword"
	
	local OwnsSwordData
	
	local success, err = pcall(function()
		OwnsSwordData = ds:GetAsync(player.UserId.."OwnedSword")
	end)
	
	if success then
		ToolFolder.OwnsSword.Value = OwnsSwordData
	else
		warn(err)
	end
	
	local sword = game.ReplicatedStorage.Tools.ClassicSword
	
	local function giveSword(player)
		if not player.Backpack:FindFirstChild("ClassicSword") then
			sword:Clone().Parent=player.Backpack
			sword:Clone().Parent=player.StarterGear
		end
	end
	game.ReplicatedStorage.ShopRemotes.BuySword.OnServerEvent:Connect(function()
		if player.leaderstats.Cash.Value >= 100 then
			if player.ToolFolder.OwnsSword.Value == true and player.ToolFolder.hasSword.Value == false then
				giveSword(player)
				player.ToolFolder.hasSword.Value = true
			elseif player.ToolFolder.OwnsSword.Value == false and player.ToolFolder.hasSword.Value == false and player.leaderstats.Cash.Value >= 100 then
				player.ToolFolder.OwnsSword.Value = true
				player.leaderstats.Cash.Value -= 100
				giveSword(player)
			end
		end
	end)
	game.ReplicatedStorage.ShopRemotes.RemoveSword.OnServerEvent:Connect(function()
		if player.Backpack:FindFirstChild("ClassicSword") then
			player.Backpack.ClassicSword:Destroy()
			player.StarterGear.ClassicSword:Destroy()
		else
			print("Nothing to remove")
		end
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	ds:SetAsync(player.UserId.."OwnedSword",player.OwnsSword.Value)
end)
Shop gui buy script(local script)
local plr = game.Players.LocalPlayer

script.Parent.BuyEquip.MouseButton1Click:Connect(function()
	if plr.leaderstats.Cash.Value >= 100 and plr.ToolFolder.hasSword.Value == false or plr.ToolFolder.OwnsSword.Value == true and plr.ToolFolder.hasSword.Value == false then
		game.ReplicatedStorage.ShopRemotes.BuySword:FireServer(plr)
		plr.ToolFolder.hasSword.Value = true
	end 
end)
script.Parent.Unequip.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.ShopRemotes.RemoveSword:FireServer()
end)
Cash leaderstats(server)
local datastore = game:GetService("DataStoreService")
local cashdata = datastore:GetDataStore("Cash")

game.Players.PlayerAdded:Connect(function(player)
	
	local playerKey = "Player_"..player.UserId
	
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	local cash = Instance.new("IntValue", leaderstats) -- MAKE SURE TO ENABLE API SERVEICE
	cash.Name = "Cash"
	
	local data
	local succses, errormessage = pcall(function()
		data = cashdata:GetAsync(playerKey)
	end)
	
	if succses then
		cash.Value = data
	else
		warn(errormessage)
	end
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	local playerKey = "Player_"..player.UserId
	
	cashdata:SetAsync(playerKey, player.leaderstats.Cash.Value)
end)

the problem is when i click buy button nothing happens no error too and the cash leaderstats wont get saved

1 Like

Why are you encasing both of your events inside the PlayerAdded event? Can’t it just be easier to reference the Player with the parameter OnServerEvent gives you?

local dss = game:GetService("DataStoreService")
local ds = dss:GetDataStore("ToolShop")

game.Players.PlayerAdded:Connect(function(player)
	
	local ToolFolder = Instance.new("Folder",player)
	ToolFolder.Name = "ToolFolder"
	
	local hasSword = Instance.new("BoolValue",ToolFolder)
	hasSword.Value = false
	hasSword.Name = "hasSword"
	
	local OwnsSword = Instance.new("BoolValue",ToolFolder)
	OwnsSword.Value = false
	OwnsSword.Name = "OwnsSword"
	
	local OwnsSwordData
	
	local success, err = pcall(function()
		OwnsSwordData = ds:GetAsync(player.UserId.."OwnedSword")
	end)
	
	if success then
		ToolFolder.OwnsSword.Value = OwnsSwordData
	else
		warn(err)
	end
	
	local sword = game.ReplicatedStorage.Tools.ClassicSword
end)

local function giveSword(player)
	if not player.Backpack:FindFirstChild("ClassicSword") then
		sword:Clone().Parent=player.Backpack
		sword:Clone().Parent=player.StarterGear
	end
end

game.ReplicatedStorage.ShopRemotes.BuySword.OnServerEvent:Connect(function(player)
	if player.leaderstats.Cash.Value >= 100 then
		if player.ToolFolder.OwnsSword.Value == true and player.ToolFolder.hasSword.Value == false then
			giveSword(player)
			player.ToolFolder.hasSword.Value = true
		elseif player.ToolFolder.OwnsSword.Value == false and player.ToolFolder.hasSword.Value == false and player.leaderstats.Cash.Value >= 100 then
			player.ToolFolder.OwnsSword.Value = true
			player.leaderstats.Cash.Value -= 100
			giveSword(player)
		end
	end
end)

game.ReplicatedStorage.ShopRemotes.RemoveSword.OnServerEvent:Connect(function(player)
	if player.Backpack:FindFirstChild("ClassicSword") then
		player.Backpack.ClassicSword:Destroy()
		player.StarterGear.ClassicSword:Destroy()
	else
		print("Nothing to remove")
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	ds:SetAsync(player.UserId.."OwnedSword",player.OwnsSword.Value)
end)

ok imma try that now but my cash datastore isnt working too

now i put the script that you gave now the hasSword value becomes true but not the ownsword

also i did enable studio api services

Like did you did your GetAsync function, you should encase this in a pcall as well to prevent it from erroring at all & outputting back so you can see what happened

Don’t just stop there from setting your data eeee

1 Like

getasync for cash is in a pcall though

You still should check to make sure, don’t stop with just 1 pcall in using DataStores

local datastore = game:GetService("DataStoreService")
local cashdata = datastore:GetDataStore("Cash")

game.Players.PlayerAdded:Connect(function(player)
	
	local playerKey = "Player_"..player.UserId
	
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	local cash = Instance.new("IntValue", leaderstats) -- MAKE SURE TO ENABLE API SERVEICE
	cash.Name = "Cash"
	
	local data
	local succses, errormessage = pcall(function()
		data = cashdata:GetAsync(playerKey)
	end)
	
	if succses then
		cash.Value = data
	else
		warn(errormessage)
	end
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	local playerKey = "Player_"..player.UserId
	
    local success, whoops = pcall(function()
	    cashdata:SetAsync(playerKey, player.leaderstats.Cash.Value)
    end)

    if success then
       print("Data successfully saved!")
    else
        warn(whoops)
    end
end)

nope it didnt work

no errors too

Edit it gives this error

OwnsSword is not a valid member of Player “Players.Creeperman16487”

You should be referencing the ToolFolder first, since you parented the OwnsSword bool in there

You confused me with your other datastore script >:(

ok but it still dont work

ok edit that i made it working now the game gives me sword and unequips it but its still doesnt save the bool value of the OwnsSword and the cash too

edit again it works great but sometimes it glitches out

test 2 (datastore).rbxl (41.6 KB)

here is the place file pls fix or find help for me thx

1 Like

Ok after 500 hours of pain and looking around, I went ahead & fixed a couple of unnecessary things you included in your scripts, I won’t go over all of them but I’ll tell you the main issue why you couldn’t retrieve the sword again upon equipped when you rejoined

  • You never included a BindToClose function in your script, which sometimes may not save if you’re in Studio

  • You forgot to set back your hasSword values to true/false when they were detecting the Tool

Fixed Version.rbxl (41.4 KB)

im sorry if you had pain lol anyways ill try this now thx for the help i appreciate alot

thx man it works so great love you!

also i like your kindness becuz you helped me fix the place file