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

DataStore not saving certain values?

I am creating a game in which players collect crabs that follow them around, and I want to be able to save what crabs the players have. To do this, I first created a value updater that activates once the crab is parented to the player.

local crab = script.Parent
local Players = game:GetService("Players")
local player = game.Players.LocalPlayer
local character = game.Players.LocalPlayer.Character

if crab.Parent == character then
	player.NormalCrabs.Value = 1
end

This works, and the player's NormalCrabs value gets set to 1. I want to be able to save that value so I can read it from an alternate script in order to summon the crab back. Here is the script in ServerScriptStorage that handles the player's crab stats, and other things.

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")


game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Soil = Instance.new("IntValue")
	Soil.Name = "Soil"
	Soil.Value = 200
	Soil.Parent = leaderstats


	local NormalCrabs = Instance.new("IntValue")
	NormalCrabs.Name = "NormalCrabs"
	NormalCrabs.Value = 0
	NormalCrabs.Parent = player

	local TallCrabs = Instance.new("IntValue")
	TallCrabs.Name = "TallCrabs"
	TallCrabs.Value = 0
	TallCrabs.Parent = player

	local FuriousCrabs = Instance.new("IntValue")
	FuriousCrabs.Name = "FuriousCrabs"
	FuriousCrabs.Value = 0
	FuriousCrabs.Parent = player

	local WideCrabs = Instance.new("IntValue")
	WideCrabs.Name = "WideCrabs"
	WideCrabs.Value = 0
	WideCrabs.Parent = player

	local ToxicCrabs = Instance.new("IntValue")
	ToxicCrabs.Name = "ToxicCrabs"
	ToxicCrabs.Value = 0
	ToxicCrabs.Parent = player

	local HermitCrabs = Instance.new("IntValue")
	HermitCrabs.Name = "HermitCrabs"
	HermitCrabs.Value = 0
	HermitCrabs.Parent = player

	local Crabs = Instance.new("IntValue")
	Crabs.Name = "Crabs"
	Crabs.Value = 0
	Crabs.Parent = leaderstats

	local Shovels = Instance.new("IntValue")
	Shovels.Name = "Shovels"
	Shovels.Value = 0
	Shovels.Parent = player


	local uniqueKey = "id-"..player.UserId
	local getSaved = myDataStore:GetAsync(uniqueKey)
	if getSaved then
		NormalCrabs.Value = getSaved[1]
		TallCrabs.Value = getSaved[2]
		FuriousCrabs.Value = getSaved[3]
		WideCrabs.Value = getSaved[4]
		ToxicCrabs.Value = getSaved[5]
		HermitCrabs.Value = getSaved[6]
		Soil.Value = getSaved[7]
	else
		local valuesToSave = {
			NormalCrabs.Value,
			TallCrabs.Value,
			FuriousCrabs.Value,
			WideCrabs.Value,
			ToxicCrabs.Value,
			HermitCrabs.Value,
			Soil.Value,
		}
		myDataStore:SetAsync(uniqueKey, valuesToSave)
	end
	end)

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

local uniqueKey = "id-"..player.UserId
	local valuesToSave = {
			player.NormalCrabs.Value,
			player.TallCrabs.Value,
			player.FuriousCrabs.Value,
			player.WideCrabs.Value,
			player.ToxicCrabs.Value,
			player.HermitCrabs.Value,
			player.leaderstats.Soil.Value,
	}
	myDataStore:SetAsync(uniqueKey, valuesToSave)
end)

Now arises the problem. The DataStore won't save the player's 'NormalCrabs','TallCrabs','FuriousCrabs',etc. values, but it will save only the coins. Just to clarify, The 'Crabs' and 'Shovels' values have been left out of the DataStore on purpose.

Here is a list of my attempts to fix the problem: The crabs were parented to the player, so I thought that might've been the problem. Upon parenting them to leaderstats, it didn't work. I thought that due to the fact that they had the name "Crabs" at the end of each one, and that there was a value named "Crabs" already somehow messed it up, but it didn't seem to matter once I changed the name of the "Crabs" value. I also figured that since I wasn't saving all of the values in the script, it might be messing up, but I tried saving all of them (including 'Crabs' and 'Shovels') to no avail. Lastly, I was using a different version of saving/loading the data, so I changed it. I kept that, but it didn't fix my problem.

I hope I was clear enough, and thank you for your time.

1 answer

✓ Accepted answer

There's many reasons why this does save Crabs

  1. Changing values in local scripts wont change the value on the server, so the value on the server would still be 0.

  2. You did not save Crabs in your valuesToSave table.

  3. You did not load your Crabs value when the player joins. (Problem 2 must be fixed first)

4 comments

0

Ah, I'm sorry. I explained it incorrectly. When I said crab values, I was referencing the 'NormalCrabs','TallCrabs','FuriousCrabs', etc. Thank you for the feedback, though.

0

OH. Well, anyways the 1st problem is still an issue.

0

I see how I can fix my problem now. Thank you for the advice!

0

np!

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