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

Data Stores will not load data. How to fix?

This is my code:

local DataStoreService = game:GetService("DataStoreService")
local PointsStore = DataStoreService:GetDataStore("BBSystems", "PointsValueHolder")

game.Players.PlayerAdded:Connect(function(Player)
	local Key = "uid_" .. Player.UserId
	local leaderstats = Instance.new("Folder")
	local Points = Instance.new("NumberValue")
	local Rank = Instance.new("StringValue")
	
	leaderstats.Parent = Player
	leaderstats.Name = "leaderstats"
	
	Points.Parent = leaderstats
	Points.Name = "Points"
	Points.Value = PointsStore:GetAsync(Key)
	
	Rank.Parent = leaderstats
	Rank.Name = "Rank"
	Rank.Value = Player:GetRoleInGroup(4494167)
end)

game.Players.PlayerRemoving:Connect(function(Player)
	local Key = "uid_" .. Player.UserId
	PointsStore:SetAsync(Key, Player.leaderstats.Points.Value)
end)

and for some reason It will not load the data up when the play joins (Im not sure if it saves it either)

If you could help, that would be much appreciated!

1 answer

try using a pcall to get the data and set it to a variable. this always works for me

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

game.Players.PlayerAdded:Connect(function(Player)
    local Key = "uid_" .. Player.UserId
    local leaderstats = Instance.new("Folder")
    local Points = Instance.new("NumberValue")
    local Rank = Instance.new("StringValue")

    leaderstats.Parent = Player
    leaderstats.Name = "leaderstats"

    Points.Parent = leaderstats
    Points.Name = "Points"
    Points.Value = aDataStore:GetAsync(Key)

    Rank.Parent = leaderstats
    Rank.Name = "Rank"
    Rank.Value = Player:GetRoleInGroup(4494167)

	local player_data_points

    pcall(function()
        player_data_points = aDataStore:GetAsync(Key)  
    end)

    if player_data_points ~= nil then
        Player.leaderstats.Points.Value = player_data_points
    end

end)

game.Players.PlayerRemoving:Connect(function(Player)
    local Key = "uid_" .. Player.UserId
    aDataStore:SetAsync(Key, Player.leaderstats.Points.Value)
end)

8 comments

0

Doesn't work..

0

It works for me. here, I'll edit it to show the final code. you need to join an actual game by the way- data won't save in studio. I actually did change a few things but it works- saves points and gets the players current group rank. accept my answer if everything works :)

0

Doesn't work.. Where is your script placed?

0

a server script under ServerScriptStorage

0

Nothing

0

I edited my answer, are you sure your doing everything? I have this very script under serverscriptstorage and it works. you must be doing something wrong.

0

I imputed the code into a script, put that script in serverscriptstorage but nothing

0

do you have discord? ill help koala#6590 i dont know why its not working for you. it works for me and it only saves IN GAME not in STUDIO

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