I highly recommend you check this out:
http://wiki.roblox.com/index.php?title=Saving_Player_Data
Here's the script with some explanations:
(Make sure your game has access to API Services, or else DataStore won't work)
DataStore = game:GetService("DataStoreService"):GetDataStore("Stats")
local GoodBoy = script.EXPNeeded.Value
SaveTime = 60
game.Players.PlayerAdded:Connect(function(plr)
local PlayerKey = "user_".. plr.userId
local stats = Instance.new("IntValue", plr)
stats.Name = "leaderstats"
if DataStore:GetAsync(PlayerKey)then
print("Loading " .. PlayerKey)
local Data = DataStore:GetAsync(PlayerKey)
print(Data.Souls)
local EXP = Instance.new("IntValue", stats)
EXP.Name = "EXP"
EXP.Value = Data.EXP
local level = Instance.new("IntValue", stats)
level.Name = "Level"
level.Value = Data.level
local Souls = Instance.new("IntValue", stats)
Souls.Name = "Souls"
Souls.Value = Data.Souls
else do
local NewStats = {EXP = 0,level = 0,Souls = 0,}
local EXP = Instance.new("IntValue", stats)
EXP.Name = "EXP"
EXP.Value = 0
local level = Instance.new("IntValue", stats)
level.Name = "Level"
level.Value = 0
local Souls = Instance.new("IntValue", stats)
Souls.Name = "Souls"
Souls.Value = 0
DataStore:SetAsync(PlayerKey,NewStats)
end
end
plr.leaderstats.EXP.Changed:Connect(function()
plr.leaderstats.Level.Value = math.floor(plr.leaderstats.EXP.Value/script.EXPNeeded.Value)
plr.leaderstats.EXP.Value = 0
GoodBoy = GoodBoy + 10
end)
while true do
wait(SaveTime)
local PlayerKey = "user_".. plr.userId
local Stats = plr.leaderstats
local Data = {}
Data.EXP = Stats.EXP.Value
Data.level = Stats.Level.Value
Data.Souls = Stats.Souls.Value
DataStore:SetAsync(PlayerKey,Data)
print("Saved Data")
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
local PlayerKey = "user_".. Player.userId
local Stats = Player.leaderstats
local Data = {}
Data.EXP = Stats.EXP.Value
Data.level = Stats.Level.Value
Data.Souls = Stats.Souls.Value
DataStore:SetAsync(PlayerKey,Data)
end)
I also recommend you read through here:
https://wiki.roblox.com/index.php?title=API:Class/GlobalDataStore (That has everything you need to know about Datastore.)
2 comments
You use datastore
i already knew it but how?