Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why Is The Following Datastore Code Not Working?

I really have no idea what I'm doing wrong! I have two functions: one for when a player enters, and one for a player leaves. Here's the one for players entering:

game.Players.PlayerAdded:connect(function(player)
	
	local stats = Instance.new("IntValue", player)
	stats.Name = "leaderstats"

	local hidden = Instance.new("IntValue", player)
	hidden.Name = "hiddenstats"
	
	local bounty = Instance.new("IntValue", stats)
	bounty.Name = "Bounty"

	local power = Instance.new("IntValue", stats)
	power.Name = "Power"
	
	local title = Instance.new("StringValue", stats)
	title.Name = "Title"
	title.Value = "None"

	local justSpawned = Instance.new("BoolValue", hidden)
	justSpawned.Name = "JustSpawned"
	justSpawned.Value = false

	local isDead = Instance.new("BoolValue", hidden)
	isDead.Name = "IsDead"
	isDead.Value = false

	local currentHealth = Instance.new("IntValue", hidden)
	currentHealth.Name = "CurrentHealth"
	currentHealth.Value = 100

	local onQuest = Instance.new("BoolValue", hidden)
	onQuest.Name = "OnQuest"
	onQuest.Value = false
	
	local key = "Player-"..player.userId
	
	local savedPlayerStats = PlayerData:GetAsync(key)

	if savedPlayerStats then
		
		bounty.Value = savedPlayerStats[1]
		power.Value = savedPlayerStats[2]
		title.Value = savedPlayerStats[3]
		justSpawned.Value = savedPlayerStats[4]
		isDead.Value = savedPlayerStats[5]
		currentHealth.Value = savedPlayerStats[6]
		onQuest.Value = savedPlayerStats[7]

	else

		local dataToSave = {bounty.Value, power.Value, title.Value, justSpawned.Value, isDead.Value, currentHealth.Value, onQuest.Value}
		PlayerData:SetAsync(key, dataToSave)
		
	end
	
end)

Here's the one for players leaving:

game.Players.PlayerRemoving:connect(function(player)
	
	local key = "Player-"..player.userId

	local stats = player:WaitForChild("leaderstats")
	local hiddenstats = player:WaitForChild("hiddenstats")

	local bounty = stats:WaitForChild("Bounty")
	local power = stats:WaitForChild("Power")
	local title = stats:WaitForChild("Title")
	
	local justSpawned = hiddenstats:WaitForChild("JustSpawned")
	justSpawned.Value = false
	local isDead = hiddenstats:WaitForChild("IsDead")
	isDead.Value = false
	local health = hiddenstats:WaitForChild("CurrentHealth")
	health.Value = 100
	local onQuest = hiddenstats:WaitForChild("OnQuest")
	onQuest.Value = false
	
	local dataToSave = {bounty.Value, power.Value, title.Value, justSpawned.Value, isDead.Value, health.Value, onQuest.Value}
	PlayerData:SetAsync(key, dataToSave)
	
end)

Please note that I did not include the code for creating the database. Anyways, I've read over my code like a thousand times, and I can't figure out what I'm doing wrong. I've tested it multiple times, manually changing my leaderstat values, but when I reenter the game, the values go back to 0.

11 comments

0

I think "leaderstats" should be a Folder or a Group object (not an IntValue) for it to display correctly in the PlayerList.

0

@KingedPawn Thank you for that note, but the problem still persists.

0

Are there any errors? If there are, I think you may need to use a pcall everytime you try to receive something from the DataStore. It may be that you're trying to retrieve too much information in a little amount of time.

0

Also, try to do some debugging with print statements. Try to print out savedPlayerStats[1] and savedPlayerStats[2] and so on to see what those values are.

0

@KingedPawn These are great suggestions. I just tried printing out values from savedPlayerStats, and nothing resulted in the output. I don't receive any errors either. This is really strange.

0

I used to have a similar problem with my game a long time ago, but I think I was able to fix it by switching to UpdateAsync instead of SetAsync. In your PlayerRemoving event, change the SetAsync to UpdateAsync (but keep it in your PlayerAdded one): http://wiki.roblox.com/index.php?title=API:Class/GlobalDataStore/UpdateAsync

0

I did this: PlayerData:UpdateAsync(key, function(oldValue) oldValue = bounty.Value local newValue = oldValue end) My problem is still not fixed. I don't know if I'll ever be able to find out what's wrong.

0

Try it like this (you have to have a return value): PlayerData:UpdateAsync(key, function(oldValue) return dataToSave end)

0

@KingedPawn I believe the problem may lie somewhere in the function for players entering. I tried printing the values for savedPlayerStats, but nothing showed up in the output. I'm starting to doubt the fact that the player data is even loaded in the first place.

0

@KingedPawn I finally found the solution! I posted it as a comment to the answer of this question.

0

Congrats. Glad you found what was wrong.

1 answer

✓ Accepted answer

I just copied and pasted your script into my game, and it works fine.

If you are finding problems in studio, just go to Configure this GAME (not place) and turn on Enable Studio Access to API Service

like: image

2 comments

0

I finally found the problem. I am part of a Team Create, and I always get this HTTP 403 error, so I requested to log onto the owner's account and edit from there, and it worked like you said! Just a quick question, how does the game owner grant access to me to solve the HTTP 403 error?

0

uh no clue. That is probably a roblox team stuff

This discussion is preserved as it was. New answers, comments, and votes are closed.