Need help with a datastore not working and/or loading in the correct Gui

I’ve been stuck on this problem for days now, and have looked at multiple resources online regarding datastores, but I still cannot find out why my code does not work. What my code below does is basically, it handles 2 datastores, one for the coins leaderstats count and the other storing a boolean value, where if it is false, the Gui’s text is “Equip” but if it is nil, nothing happens and the Gui loads in like it is for a new player (It loads in “Buy” as the Gui text if the player is new and did not buy the Trail yet). Please feel free to leave any comments down below if you know a solution for this problem. Thanks!

local currencyName = "Coins"
local DataStore = game:GetService("DataStoreService"):GetDataStore("TestDataStore")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Events = ReplicatedStorage:WaitForChild("Events")
local BooleanEvent = Events:WaitForChild("BooleanEvent")
local DataStoreService = game:GetService("DataStoreService")
local RedTrailsStore = DataStoreService:GetDataStore("Trails")
local Players = game:GetService("Players")
local Redbutton = game.StarterGui.ShopGui.TrailsShopFrame.ScrollingFrame.Template.BuyButton.RedTrail

game.Players.PlayerAdded:Connect(function(player)

	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = player	

	local currency = Instance.new("IntValue")
	currency.Name = currencyName
	currency.Parent = folder

	local ID = currencyName.."-"..player.UserId
	local playerKey = "Player_" .. player.UserId
	local savedData = nil	
	local redtraildata = nil

	pcall(function()
		savedData = DataStore:GetAsync(ID)
		redtraildata = RedTrailsStore:GetAsync(playerKey)
	end)

	if savedData ~= nil then
		currency.Value = savedData
		print("Data loaded")
	else
		-- New player
		currency.Value = 10
		print("New player to the game")
	end
	
	if redtraildata ~= nil then
		local Redbutton = game.StarterGui.ShopGui.TrailsShopFrame.ScrollingFrame.Template.BuyButton.RedTrail
		Redbutton.Parent.ImageColor3 = Color3.fromRGB(252, 1, 7)
		Redbutton.Text = "Equip"
	else
		print("it is nil")
		Redbutton.Text = "Buy"
	end


	
end)



game.Players.PlayerRemoving:Connect(function(player)
	local ID = currencyName.."-"..player.UserId
	local playerKey = "Player_" .. player.UserId
	local Redbutton = game.StarterGui.ShopGui.TrailsShopFrame.ScrollingFrame.Template.BuyButton.RedTrail
	DataStore:SetAsync(ID,player.leaderstats[currencyName].Value)
	
	if Redbutton.Parent.ImageColor3 == Color3.fromRGB(252, 1, 7) then
		RedTrailsStore:UpdateAsync(playerKey, false)
	else 
		RedTrailsStore:UpdateAsync(playerKey, nil)
	end
end)

game:BindToClose(function()

	-- When game is ready to shutdown

	for i, player in pairs(game.Players:GetPlayers()) do
		if player then
			player:Kick("This game is shutting down")
		end
	end

	wait(5)	

end)

UpdateAsync expects a function that returns the data as its second argument. In your case, you used a boolean.

ds:UpdateAsync(key, function() 
    return false 
end)

Hey, thanks for the reply. Do you think I should use SetAsync instead of UpdateAsync or should I just stick with UpdateAsync?

If you need to use the old data, use UpdateAsync. If not, you can use SetAsync.

Okay, then I think SetAsync would be a better choice for my block of code. How would I use SetAsync to set a playerkey’s value to a boolean value?

ds:SetAsync(key, true)

simple as that

So I tried out using SetAsync, but for some reason after buying the trail in my first playing session, it wouldn’t save and set the Buy button to Equip in my second playing session after leaving and rejoining. Here is my new code shown below:

local currencyName = "Coins"
local DataStore = game:GetService("DataStoreService"):GetDataStore("TestDataStore")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Events = ReplicatedStorage:WaitForChild("Events")
local BooleanEvent = Events:WaitForChild("BooleanEvent")
local DataStoreService = game:GetService("DataStoreService")
local RedTrailsStore = DataStoreService:GetDataStore("Trails")
local Players = game:GetService("Players")
local Redbutton = game.StarterGui.ShopGui.TrailsShopFrame.ScrollingFrame.Template.BuyButton.RedTrail

game.Players.PlayerAdded:Connect(function(player)

	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = player	

	local currency = Instance.new("IntValue")
	currency.Name = currencyName
	currency.Parent = folder

	local ID = currencyName.."-"..player.UserId
	local playerKey = "Player_" .. player.UserId
	local savedData = nil	
	local redtraildata = nil

	pcall(function()
		savedData = DataStore:GetAsync(ID)
		redtraildata = RedTrailsStore:GetAsync(playerKey)
	end)

	if savedData ~= nil then
		currency.Value = savedData
		print("Data loaded")
	else
		-- New player
		currency.Value = 10
		print("New player to the game")
	end
	
	if redtraildata ~= nil then
		local Redbutton = game.StarterGui.ShopGui.TrailsShopFrame.ScrollingFrame.Template.BuyButton.RedTrail
		Redbutton.Parent.ImageColor3 = Color3.fromRGB(252, 1, 7)
		Redbutton.Text = "Equip"
	else
		print("it is nil")
		Redbutton.Text = "Buy"
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local ID = currencyName.."-"..player.UserId
	local playerKey = "Player_" .. player.UserId
	local Redbutton = game.StarterGui.ShopGui.TrailsShopFrame.ScrollingFrame.Template.BuyButton.RedTrail
	DataStore:SetAsync(ID,player.leaderstats[currencyName].Value)
	
	if Redbutton.Parent.ImageColor3 == Color3.fromRGB(252, 1, 7) then

		RedTrailsStore:SetAsync(playerKey, false)
	else 

		RedTrailsStore:SetAsync(playerKey, nil)
	end
end)

game:BindToClose(function()

	-- When game is ready to shutdown

	for i, player in pairs(game.Players:GetPlayers()) do
		if player then
			player:Kick("This game is shutting down")
		end
	end
	wait(5)	
end)

This check will never work.
Mainly because you use

which is the starter gui and not player gui, and also the server will not see the change in the frame (when it gets changed on the local script, due to FE). Suggest using remote events when the player purchases it.

Wow, I’ve never thought of that. I’m working on it right now, and once I’m done, I’ll let you know if it works! Thank you!