Whats an efficent way for me to save data when someone purchases something ingame with coins

Right now I’m trying to figure out a way to save a bool value but also have it so I can change the value of the bool value to check if they have the item. The thing is I’ve saved the bool value into my data2 folder but I’m having trouble saving the value for the player when its changed. If the value Is true than the people has the animation, if the value is false then they don’t and so on with the rest.

Video:

Here the DatastoreScript:

local datastoreservice = game:GetService("DataStoreService")
local datastore = datastoreservice:GetDataStore("Datastore1")
local PlayerData = game:GetService("ServerStorage").PlayerData:Clone()

local loaded = {}
game.Players.PlayerAdded:Connect(function(plr)
	

	local success, value = pcall(datastore.GetAsync, datastore, plr.UserId)
	if success == false then warn(value) return end
	
	local data = value or {} -- if data is data is equal to something, or the player has some data then it'll get it. If not then it'll make a empty table.
	print("Loaded:", value)
	
	local Data1 = Instance.new("Folder")
	Data1.Name = "Data1"
	Data1.Parent = PlayerData
	
	local Data2 = Instance.new("Folder")
	Data2.Name = "Data2"
	Data2.Parent = PlayerData
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = PlayerData
	
	local Coins  = Instance.new("IntValue")
	Coins.Name = "Coins"
	Coins.Parent = leaderstats
	
	local Speed = Instance.new("IntValue")
	Speed.Name = "Speed"
	Speed.Parent = leaderstats
	
	local LoginCounter = Instance.new("IntValue")
	LoginCounter.Name = "LoginCounter"
	LoginCounter.Parent = Data2
	
	local NarutoRun = Instance.new("BoolValue")
	NarutoRun.Name = "NarutoRun"
	NarutoRun.Value = false
	NarutoRun.Parent = Data2
	
	for i, folder in PlayerData:GetChildren() do
		local subdata = data[folder.Name] or {} -- the subdata will try to equal to the data's name, but if it is nil then it'll make a empty table
		local clone = folder:Clone()
		
		for i, child in clone:GetChildren() do
			child.Value = subdata[child.Name] or child.Value-- getting whatever int or bool value inside the playerdata folders, and equalling it to the current value
		end
		clone.Parent = plr
	end
	loaded[plr] = true
end)





game.Players.PlayerRemoving:Connect(function(Player)
	
	if loaded[Player] == nil then return end
	local data ={}
	
	for i, folder in PlayerData:GetChildren() do
		local subData = {}
		for i, child in Player[folder.Name]:GetChildren() do
			subData[child.Name] = child.Value
		end
		data[folder.Name] = subData
		folder:Destroy()
	end
	local success, value = pcall(datastore.SetAsync, datastore, Player.UserId, data)
	print("Saved:", data)
	loaded[Player] = nil
end)


game:BindToClose(function()
	while next(loaded) ~= nil do
		task.wait()
	end
end)

Heres the store purchase script:

local player = game:GetService("Players").LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local RunAnimation = script:WaitForChild("Animation[Naruto]")
local Track = Humanoid:LoadAnimation(RunAnimation)

local NarutoBool = game.Players.LocalPlayer:WaitForChild("Data2"):WaitForChild("NarutoRun")
local TextButton = script.Parent
local Speed = game.Players.LocalPlayer.leaderstats.Speed
local FormEvent = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvents"):WaitForChild("FormsRemote")
TextButton.MouseButton1Click:Connect(function()
	if Speed.Value > 1000 then 
		local NarutoPurchased = Instance.new("BoolValue")
		NarutoPurchased.Name = "Purchased"
		NarutoPurchased.Parent = player:FindFirstChild("Data2")
		
		NarutoBool.Value = true
		print("Purchased")
		Speed.Value = Speed.Value - 1000
		
		end
		if NarutoBool.Value == true then
		
		for _, Animations in pairs(Humanoid:GetPlayingAnimationTracks()) do
			Animations:Stop()

		end
		Track:Play()
	elseif Speed.Value < 1000 then

		print("Unsufficent Funds")
	end
end)

Another attempt for me to save the data when the player is added:

game.Players.PlayerAdded:Connect(function(player)
	local Data2 = player:WaitForChild("Data2", 10)
	if Data2 == nil then return end
	
	if Data2:FindFirstChild("NarutoPurchased") ~= nil then
		local NarutoBool = Data2:WaitForChild("NarutoRun") 
		NarutoBool.Value = true
	end
end)

1 Like