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?
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.
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.