Problems with Database

I want a simple system with a leaderboard which saves data to a database.

For some reason the database is not properly working. When I do checks it returns nil, although it does give me a confirmation each time it saves data.

I’ve tried asking a friend and looked a bit on the forum but haven’t found anything useful so far.
Both scripts are listed below, thanks for reading!
-Maxi

PlayerData Module Script

local PlayerData = {}

local DataStoreService = game:GetService("DataStoreService")
local PlayerService = game:GetService("Players")

local storedData = DataStoreService:GetDataStore("idk")

playerID = nil

function PlayerData.getData(playerID) 
	local success, errorMsg = pcall(function()
				
		return storedData:GetAsync(playerID)
	end)
	
	if success  then
		print(playerID .. " was successfully requested by the server value: " .. storedData:GetAsync(playerID))
	end

	if not success then
		print(playerID .. "'s Data was attempted to be requested " + errorMsg)
	end
end


function PlayerData.setData(playerID, value)
	local success, errormsg = pcall(function() 
		storedData:SetAsync(playerID, value)
		
	end)
	
	if success then
		print(value .. " was set for player ID: " .. playerID .. "/" .. PlayerService:GetPlayerByUserId(playerID).Name)
	end
	
	if not success then
		print("There was an error setting " .. value .. " for player ID: " .. playerID .. "/" .. PlayerService:GetPlayerByUserId(playerID).Name)
	end
end

return PlayerData


Main Script

local DSS = game:GetService("DataStoreService")
local StarterGUI = game:GetService("StarterGui")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlayerData = require(game.ServerScriptService.PlayerData)

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


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

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

	print(PlayerData.getData(player.UserId))

	if (PlayerData.getData(player.UserId) ~= nil) then
		print(player.Name .. " is already stored in Database with value " .. PlayerData.getData(player.UserId))
		PlayerData.setData(player.UserId, PlayerData.getData(player.UserId) + 1)
		stuff.Value = PlayerData.getData(player.UserId)


	else
		print(player.Name .. " is currently not stored in Database")
		PlayerData.setData(player.UserId, 1)
		stuff.Value = PlayerData.getData(player.UserId)
	end

	player.Chatted:Connect(function(msg)
		local loweredText = string.lower(msg)
		if string.find(loweredText, "increase") then


			PlayerData.setData(player.UserId, stuff.Value + 1)
			wait()
			stuff.Value = (PlayerData.getData(player.UserId))

			ReplicatedStorage.ChatEvent:FireClient(player.Name.. "'s stuff got changed from " .. stuff.Value - 1 .. " to " .. stuff.Value)

		elseif  string.find(loweredText, "check") then
			print(PlayerData.getData(player.UserId))
			ReplicatedStorage.ChatEvent:FireAllClients("Current Value: " .. PlayerData.getData(player.UserId))


		end
	end)
end)
1 Like

errorMsg in a GetAsync is normally written as “data”, since this is the thing returning your data.
If it returns nil, that means the GetAsync request was successful, but there’s no data saved to the key, hence “nil”.
What you’re trying to do is add “to be requested” a string, with “+ errorMsg” a “nil” data, this is not possible.
Try instead: This separates the data types in the print statement

 print("data was...", errorMsg)

You may have an error like this in other places, adding a comma instead of a + or …, is always safest when working with possible multiple data types.

If no data is found, (it returns nil), you should create a datatable or value to be saved for the player which you can then update as you go.

1 Like

add return success within the if success then code contained within PlayerData.getData()

2 Likes

Thank you for the replies!
It unfortunately didn’t fix the actual issue, however I did manage to fix it myself.
I haven’t 100% understood why it was not working before.
Instead of immediately returning the GetAsync I used a variable to save the value.

foundValue = storedData:GetAsync(playerID)

And returned the value of the variable in the if success statement:

if success  then
		print(playerID .. " was successfully requested by the server value: " .. foundValue)
		return foundValue
	end

Have a good evening:)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.