Error with DataStore as not being Valid upon the Player's Leaderstats:

Hello,

Recently, I have followed a guide on how you can construct in the code-lines DataStore at Roblox Studio. I made the script within ServerScriptService and everything was structured properly:

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local CoinsStore = DataStoreService:GetDataStore("CoinsStore")

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

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Coins = Instance.new("IntValue")
	Coins.Name = "Coins"
	Coins.Parent = leaderstats

	local info
	local success, errorMessage = pcall(function()
		info = CoinsStore:GetAsync(player.UserId.."-Coins", player.leaderstats.Coins.Value)
	end)

	if success then
		Coins.Value = info
	else
		print("Something went wrong obtaining the data.")
		warn(errorMessage)
	end

end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, errorMessage = pcall(function()
		CoinsStore:SetAsync(player.UserId.."-Coins", player.leaderstats.Coins.Value)
	end)

	if success then
		print("CoinsStore = True")
	else
		print("CoinsStore = false")
		warn(errorMessage)
	end

end)

Furthermore, I also used this script for another model (located in the game.Workspace) to add one point to the leaderstats’ value. As testing the Baseplate, I encountered major problems for both statings that DataStore is not a valid member for Players.(player.Name).leaderstats:

image

Is there any possibility to solve this problem?

Thank you!

Change player.leaderstats.Coins to just Coins

1 Like

I have changed this and it appears that it prints stating that it cannot save through the DataStore, plus still printing an error to the model’s script:

image

local Coin = script.Parent
local Sound = Coin["Coin Throws 7 (SFX)"]
local db = false
local TweenService = game:GetService("TweenService")

local Info1 = TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut, 1, false, 0)

local Goals1 = {
	Size = Vector3.new(2, 2, 0.401);
	Transparency = 1
}

local Info2 = TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut, 1, false, 0)

local Goals2 = {
	Size = Vector3.new(3, 3, 0.602);
	Transparency = 1
}

local CoinTween1 = TweenService:Create(Coin, Info1, Goals1)
local CoinTween2 = TweenService:Create(Coin, Info2, Goals2)

local Players = game:GetService("Players")

while task.wait(0.001) do
	Coin.CFrame = Coin.CFrame * CFrame.Angles(0, 0.1, 0)
	local function onTouched(Coin)
		local player = Players:GetPlayerFromCharacter(Coin.Parent)
		if not player then return end
		if db == false then
			db = true
			local Coin = script.Parent
			Coin.CanTouch = false
			Coin.CanCollide = false
			CoinTween1:Play()
			player.leaderstats.Coins.Value = player.leaderstats.Coin.Value + 1
			Sound:Play()
			
			task.wait(5)
			
			CoinTween2:Play()
			db = false
		end
	end
	Coin.Touched:Connect(onTouched)
end

task.wait(5)

Coin.Transparency = 0
Coin.CanTouch = true
Coin.CanCollide = true

@DasKairo I tried applying what you said above to the script of the model, yet everything remains with errors as analysing through the Output…

CoinsStore:GetAsync(player.UserId.."-Coins", player.leaderstats.Coins.Value)

Why are you using player.leaderstats.Coins.Value as the second value. GetAsync requires just the key, so putting the 2nd param is pointless and it contributing to the error.

Does the code work if you reduce it to just

CoinsStore:GetAsync(player.UserId.."-Coins")
1 Like

Nope, it still does not work:

image

I am pretty sure proper syntax is:

CoinsStore:GetAsync(player.UserId)

Let me check though.

Nevermind, it is saved as player.UserId.."-Coins"

1 Like

That error is not possible if you made the changes I suggested, as the bit of code I marked for removal is the only place that mentions leaderstats.Coins.

Is this for when they are joining or leaving?

This happens once leaving the experience… When it prints CoinsStore = false, it means that it couldn’t save.

Try this:

Players.PlayerAdded:Connect(function(player)

	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	local Coins = Instance.new("IntValue", leaderstats)
	Coins.Name = "Coins"

	local success, Data = pcall(function()
		return CoinsStore:GetAsync(player.UserId.."-Coins") -- Returns Data
	end)

if success then -- if Accessed DataStores
       if Data then -- if Player has Data
		Coins.Value = Data
	   else -- if no Data found
		print("No Data Found")
        Coins.Value = 0
	end
else -- If Failed to access DataStores
warn("Failed to Access DataStores")
-- maybe Kick the Player or Retry?
end

end)

1 Like

I tried and it works! But, does it get the data fully if the player leaves the game?