Trouble with DataStores

I’m trying to get my DataStore script to work but it is currently non functional and gives no errors which confuses me. Due to no errors I’m considering being because I am in Studio but I don’t think it is because I have “Enable Studio Access to API Services” enabled.

I’ve messed with the variables a bit but I’m not entirely sure what I’m doing as this is my first time interacting with DataStores.

local DataStoreService = game:GetService("DataStoreService")

local LevelData = DataStoreService:GetDataStore("LevelData")
local ExpData = DataStoreService:GetDataStore("ExpData")
local WinsData = DataStoreService:GetDataStore("WinsData")
local KarmaData = DataStoreService:GetDataStore("KarmaData")


function PlayerAdded(Player)
	
	-- Leaderstats
	
	local leaderstats = Instance.new("Folder", Player)
	leaderstats.Name = "leaderstats"
	
	local Level = Instance.new("NumberValue", leaderstats)
	Level.Name = "Level"
	Level.Value = 1
	
	local Exp = Instance.new("NumberValue", leaderstats)
	Exp.Name = "Exp"
	Exp.Value = 0
	
	local Wins = Instance.new("NumberValue", leaderstats)
	Wins.Name = "Wins"
	Wins.Value = 0
	
	local Karma = Instance.new("NumberValue", leaderstats)
	Karma.Name = "Karma"
	Karma.Value = 0
	
	local RequiredExp = Instance.new("NumberValue", leaderstats)
	RequiredExp.Name = "RequiredExp"
	RequiredExp.Value = Level.Value * 1000
	
	-- Level Up Function
	
	Exp.Changed:Connect(function()
		if Exp.Value >= RequiredExp.Value then
			Exp.Value = 0
			Level.Value += 1
			RequiredExp.Value = Level.Value * 1000
		end
	end)
	
	-- Data Store
	
	local Level = LevelData:GetAsync(Player.UserId)
	local Exp = ExpData:GetAsync(Player.UserId)
	local Wins = WinsData:GetAsync(Player.UserId)
	local Karma = KarmaData:GetAsync(Player.UserId)
end

function PlayerRemoving(Player)
	LevelData:SetAsync(Player.UserId, Player.leaderstats.Level.Value)
	ExpData:SetAsync(Player.UserId, Player.leaderstats.Exp.Value)
	WinsData:SetAsync(Player.UserId, Player.leaderstats.Wins.Value)
	KarmaData:SetAsync(Player.UserId ,Player.leaderstats.Karma.Value)
end

game.Players.PlayerAdded:Connect(PlayerAdded)
	
game.Players.PlayerRemoving:Connect(PlayerRemoving)

In short, what is wrong with my code and why is it not giving me an error?

1 Like

Try changing this to

game:GetService('Players')

instead of just

game.Players

This might work IDK

This had the same results as before.
Does changing game.Players to game game:GetService(‘Players’) have any effect on what the script does?

Ok I suggest you try figuring out where it is going wrong. Try putting PRINT in different places in your script. Then see if it prints. Keep doing this till it doesnt print anything so you can find a line that doesnt work.
(Then show us where that line is)

I suggest adding :BindToClose as well so that if the server shutdowns, it saves every player’s data since your code only saves upon leaving the game so if the server shuts down, everyone’s data won’t save.

1 Like

You haven’t loaded any values what so ever nor set the value that is in the database.
For example:

function PlayerAdded(Player)
	
	-- Leaderstats
	
	local leaderstats = Instance.new("Folder", Player)
	leaderstats.Name = "leaderstats"
	
	local Level = Instance.new("NumberValue", leaderstats)
	Level.Name = "Level"
	
	local Exp = Instance.new("NumberValue", leaderstats)
	Exp.Name = "Exp"
	
	local Wins = Instance.new("NumberValue", leaderstats)
	Wins.Name = "Wins"
	
	local Karma = Instance.new("NumberValue", leaderstats)
	Karma.Name = "Karma"
	
	local RequiredExp = Instance.new("NumberValue", leaderstats)
	RequiredExp.Name = "RequiredExp"
	
	-- Level Up Function
	
	Exp.Changed:Connect(function()
		if Exp.Value >= RequiredExp.Value then
			Exp.Value = 0
			Level.Value += 1
			RequiredExp.Value = Level.Value * 1000
		end
	end)
	
	-- Data Store
	
	local LevelD = LevelData:GetAsync(Player.UserId)
	local ExpD = ExpData:GetAsync(Player.UserId)
	local WinsD = WinsData:GetAsync(Player.UserId)
	local KarmaD = KarmaData:GetAsync(Player.UserId)

    Level.Value = LevelD or 0
    Exp.Value = ExpD or 0
    Wins.Value = WinsD or 0
    Karma.Value = KarmaD or 0
    RequiredExp.Value = Level.Value * 1000
end

Also there are many things could be better such as saving data all in one places.

1 Like

No it does not. By using GetService, it checks if the service exists, if the service does not yet exist it will be created and the new service is returned.

Yes probably what @gIwwinnq
Right now nothing is going to happen if you use this. It wont print anything nor do anything but load it.

So it wont tell you if it loads considering nothing changes

Thanks. This worked. What is better than saving all of the data in one place? Where/What else could I use to save the data?

I will add this, thanks for telling me.

1 Like