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.

4 comments
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.
OH. Well, anyways the 1st problem is still an issue.
I see how I can fix my problem now. Thank you for the advice!
np!