Hello everyone!
I’ve run into a problem, when I print the value of a leaderstat it says it has 1, but when I check the display board it says 0.
This is the code I’m using to print
local function loadLevel()
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = player:WaitForChild("leaderstats")
local lukeLevel = leaderstats:WaitForChild("LukeLevel").Value
wait(1)
print(lukeLevel)
if lukeLevel == 1 then
print("Player is on level 1")
elseif lukeLevel == 2 then
print("Player is on level 2")
end
end)
end
loadLevel()
Thanks for any help
How are the leaderstats objects being created?
local DataStoreService = game:GetService("DataStoreService")
local levelDataStore = DataStoreService:GetDataStore("LevelDataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local LukeLevel = Instance.new("IntValue")
LukeLevel.Name = "LukeLevel"
LukeLevel.Value = 1
LukeLevel.Parent = leaderstats
local ObiWanLevel = Instance.new("IntValue")
ObiWanLevel.Name = "ObiWanLevel"
ObiWanLevel.Value = 0
ObiWanLevel.Parent = leaderstats
local AnakinLevel = Instance.new("IntValue")
AnakinLevel.Name = "AnakinLevel"
AnakinLevel.Value = 0
AnakinLevel.Parent = leaderstats
local data
local success, errormessage = pcall(function()
data = levelDataStore:GetAsync(player.UserId.."-LukeLevel")
end)
if success then
LukeLevel.Value = data
else
print("There was an error whilst getting data")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
levelDataStore:SetAsync(player.UserId.."-LukeLevel",player.leaderstats.LukeLevel.Value)
end)
if success then
print("Data successfully saved!")
else
print("There was an error while saving data.")
warn(errormessage)
end
end)
The LukeStory script is getting the leaderstats value from LukeLevel before the DataStore value is set to it.
In the LukeStory script, do this:
local function loadLevel()
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = player:WaitForChild("leaderstats")
local lukeLevel = leaderstats:WaitForChild("LukeLevel")
task.wait(1)
lukeLevel = lukeLevel.Value -- gets the value *after* waiting
print(lukeLevel)
if lukeLevel == 1 then
print("Player is on level 1")
elseif lukeLevel == 2 then
print("Player is on level 2")
end
end)
end
loadLevel()
This worked but it says I have 0 on the lukelevel even though I changed it to be 1
Are you changing the value after the script runs? If so, you’d need to create a signal to listen for when the value of LukeLevel changes. Otherwise, I’m not sure what you’re saying