I have a data store currently and this data store currently consists of player’s gold, player’s level and players experience points.
when a quest is completed, a local script gives experience points to the player and the player earns levels.
This works fine in studio as it should, but in the game client it does not change the value.
I have looked over my scripts and everything is functional like i said in studio, but the game client does not change that value… I noticed the datastore was not saving the data so looking into it i found the value is not changing in the client. The experience value stays at 500 - however my UI reflects as it should.
Let me know if there is more information needed here…
-- GET DATA STORE SERVICE--
local DataStoreService = game:GetService("DataStoreService")
--------------------------------------------
--DATA STORES--
local GoldStore = DataStoreService:GetDataStore("PlayerGold")
local LevelStore = DataStoreService:GetDataStore("PlayerLevel")
local ExperienceStore = DataStoreService:GetDataStore("PlayerExperience")
--------------------------------------------
-- ON PLAYER ADDED --
game.Players.PlayerAdded:Connect(function(player)
--DATA VARIABLES SET AS NIL VALUE FOR NOW, GET UPDATED BELOW --
local GoldCoinsData
local LevelData
local ExpData
wait()
-- LEAVE THE STAT VARIABLES AS GLOBAL (NON LOCAL VARIABLES) SO WHOLE SCRIPT CAN USE --
-- LEADERSTATS --
leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
-- ADD LEADERSTATS BELOW --
GoldCoins = Instance.new("IntValue")
GoldCoins.Name = "GoldCoins"
GoldCoins.Parent = leaderstats
GoldCoins.Value = 0
Level = Instance.new("IntValue")
Level.Name = "Level"
Level.Parent = leaderstats
Level.Value = 1
-- PLAYER STATS --
playerStats = Instance.new("Folder")
playerStats.Name = "playerStats"
playerStats.Parent = player
-- ADD PLAYER STATS BELOW --
Experience = Instance.new("IntValue")
Experience.Name = "Experience"
Experience.Value = 0
Experience.Parent = playerStats
maxExperience = Instance.new("IntValue")
maxExperience.Name = "maxExperience"
maxExperience.Value = 1100
maxExperience.Parent = playerStats
--------------------------------------------
-- RESOURCES --
resourceStats = Instance.new("Folder")
resourceStats.Name = "resourceStats"
resourceStats.Parent = player
-- ADD RESOURCES BELOW --
--------------------------------------------
-- ITEMS --
itemStats = Instance.new("Folder")
itemStats.Name = "itemStats"
itemStats.Parent = player
-- ADD ITEMS BELOW --
--------------------------------------------
-------------------------------------------
local success, errormessage = pcall(function()
GoldCoinsData = GoldStore:GetAsync(player.UserId.."-GoldCoins")
LevelData = LevelStore:GetAsync(player.UserId.."-Level")
ExpData = ExperienceStore:GetAsync(player.UserId.."-Experience")
end)
if success then -- if success then sets a the value to DATA variable
GoldCoins.Value = GoldCoinsData
Experience.Value = ExpData
if LevelData then
Level.Value = LevelData
end
else
warn(errormessage) -- if fails then gives error
end
end)
-- ON PLAYER REMOVE --
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
GoldStore:SetAsync(player.UserId.."-GoldCoins", player.leaderstats.GoldCoins.Value)
LevelStore:SetAsync(player.UserId.."-Level", player.leaderstats.Level.Value)
ExperienceStore:SetAsync(player.UserId.."-Experience", player.playerStats.Experience.Value)
end)
if success then -- if success then the data gets saved via SetAsync above
print("DATA SAVED")
else
warn(errormessage) -- if fails then gives error
end
end)
-- LOOP ON PLAYER ADDED TO SAVE DATA EVERY 120 SECONDS)
game.Players.PlayerAdded:Connect(function(player)
while true do
wait(300) -- SAVE INTERVAL (SAFTEY IN CASE SERVER CRASHES, ETC)
local success, errormessage = pcall(function()
GoldStore:SetAsync(player.UserId.."-GoldCoins", player.leaderstats.GoldCoins.Value)
LevelStore:SetAsync(player.UserId.."-Level", player.leaderstats.Level.Value)
ExperienceStore:SetAsync(player.UserId.."-Experience", player.playerStats.Experience.Value)
end)
if success then -- if success then the data gets saved via SetAsync above
print("DATA SAVED")
else
warn(errormessage) -- if fails then gives error
end
end
end)