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)