DataStore reading, but not saving data?

Hello, I’m making a bounty system, so when a player kills another player, the player gets a bounty. The bounty system works, but when it comes to the DataStore it does not work.

I can’t seem to find the issue, though I’ve told the script to warn() if the data saves or reads, successfully or fails. It reads the data (GetAsync()) but doesn’t save it (SaveAsync())

Here is the script:

local players = game:GetService("Players")

local dataStoreService = game:GetService("DataStoreService")
local bountyStore = dataStoreService:GetDataStore("PlayerBounty")



players.PlayerAdded:Connect(function(plr)
	
	
	local leaderboard = Instance.new("Folder")
	leaderboard.Name = "leaderstats"
	leaderboard.Parent = plr
	
	local bountyAmount = Instance.new("IntValue")
	bountyAmount.Name = "BountyAmount"
	bountyAmount.Parent = plr
	
	local data
	local success, fail = pcall(function()
		local data = bountyStore:GetAsync(plr.UserId)
	end)
	
	if success then
		plr.BountyAmount.Value = data
		warn("Successfully read "..plr.Name.."'s bounty!")
	else
		warn("Couldn't read "..plr.Name.."'s bounty :(")
		warn(fail)
	end


	local bountyLeader = Instance.new("StringValue")
	bountyLeader.Name = "Bounty"
	bountyLeader.Parent = leaderboard
	bountyLeader.Value = "$" .. plr.BountyAmount.Value
	
	
	plr.CharacterAdded:Connect(function(char)
		
		
		local hum = char:WaitForChild("Humanoid")
		
		
		hum.Died:Connect(function()
			
			
			local killer = hum:WaitForChild("creator").Value
			killer.BountyAmount.Value = killer.BountyAmount.Value + math.random(35, 57)
			killer.leaderstats.Bounty.Value = "$" .. killer.BountyAmount.Value
			
			
		end)
	end)
end)
--


players.PlayerRemoving:Connect(function(plr)
	local success, fail = pcall(function()
		bountyStore:SetAsync(plr.UserId,plr.BountyAmount.Value)
	end)
	
	if success then
		warn("Saved "..plr.Name.."'s bounty!")
	else
		warn("Couldn't save "..plr.Name.."'s bounty :(")
		warn(fail)
	end
end)

And the output only warns this part:

warn("Successfully read "..plr.Name.."'s bounty!")
local players = game:GetService("Players")

local dataStoreService = game:GetService("DataStoreService")
local bountyStore = dataStoreService:GetDataStore("PlayerBounty")



players.PlayerAdded:Connect(function(plr)
	
	
	local leaderboard = Instance.new("Folder")
	leaderboard.Name = "leaderstats"
	leaderboard.Parent = plr
	
	local bountyAmount = Instance.new("IntValue")
	bountyAmount.Name = "BountyAmount"
	bountyAmount.Parent = plr
	
	local data
	local success, fail = pcall(function()
		local data = bountyStore:GetAsync(plr.UserId)
	end)
	
	if success then
		bountyAmount.Value = data
		warn("Successfully read "..plr.Name.."'s bounty!")
	else
		warn("Couldn't read "..plr.Name.."'s bounty :(")
		warn(fail)
	end


	local bountyLeader = Instance.new("StringValue")
	bountyLeader.Name = "Bounty"
	bountyLeader.Parent = leaderboard
	bountyLeader.Value = "$" .. plr.BountyAmount.Value
	
	
	plr.CharacterAdded:Connect(function(char)
		
		
		local hum = char:WaitForChild("Humanoid")
		
		
		hum.Died:Connect(function()
			
			
			local killer = hum:WaitForChild("creator").Value
			killer.BountyAmount.Value = killer.BountyAmount.Value + math.random(35, 57)
			killer.leaderstats.Bounty.Value = "$" .. killer.BountyAmount.Value
			
			
		end)
	end)
end)
--


players.PlayerRemoving:Connect(function(plr)
	local success, fail = pcall(function()
		bountyStore:SetAsync(plr.UserId,plr.BountyAmount.Value)
	end)
	
	if success then
		warn("Saved "..plr.Name.."'s bounty!")
	else
		warn("Couldn't save "..plr.Name.."'s bounty :(")
		warn(fail)
	end
end)

maybe try this

I see the problem, in this part

you forgot to mention a key. Here’s a simple fix:

BountyStore:SetAsync(plr.UserId .. "-bounty" .. plr.BountyAmount.Value)
Same with

Do

local data = bountyStore:GetAsync(plr.UserId.. "-bounty")

thats wrong,you can have userid as key

Really, I’m just comparing it to my data store code. I’m not really sure if it’s required or not.

Hmm, but the key is the UserId? I would only use keys if I was storing multiple values.?

Never mind “-bounty” is not a key actually. It’s something else.

I’m new to coding so I might mistake some things as a key.

I can’t see what you changed can you point it out?

“-bounty” is just an addition to the player’s UserId, it would become useful if you wanted to save multiple values in a single script.

Wait a second. I think I see the issue.

You said plr.BountyAmount.Value It’s supposed to be plr.leaderstats.BountyAmount.Value

No that’s not the issue. IntValue “BountyAmount” is parented to the player while another one is parented to leaderstats

How are you supposed to know the value of bountyamount if it’s not in leaderstats.

	local bountyLeader = Instance.new("StringValue")
	bountyLeader.Name = "Bounty"
	bountyLeader.Parent = leaderboard
	bountyLeader.Value = "$" .. plr.BountyAmount.Value

Are you trying this in Studio or in Game?

If in the studio make sure you have the “Enable Studio Access to API Services” option enabled in the Security in-game settings.

You should also try saving the data in the Game. It may be that it’s working but not saving in the studio. I was having this issue yesterday however if you make sure it has time to be called it will likely work, presuming the syntax and logic is correct.

I’ve tried both in Studio and game. Also making my friend join so the server wouldn’t close and have time to save, but nothing.

A lot of people right now are having issues with the DataStoreService, maybe it’s a global thing but we can’t be sure, I have not heard any complaints from some of the bigger games right now

Ill try the code in studio, I think Datastore should be working fine, mine was working this morning.

Alright, and yes thanks! I will try another simple datastore code, to see if its globally.

image

Just one thing to note which may instantly fix it for you, you have a second local variable called data. This will prevent the data ever reaching the “if success then” statement as it will no longer exist at that point.