I want the script server to save and load the number of points a player gets per second
the script server does not save the number of player points, I made a line that outputs everything that is in the save, but for some reason it returns a nil
I have enabled access to API services, but it still does not work.
here is the script that does it all
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local experienceStore = DataStoreService:GetDataStore("PlayerExperience")
Players.PlayerAdded:Connect(function(P)
local LS = Instance.new("Folder", P)
LS.Name = "leaderstats"
local Points = Instance.new("IntValue", LS)
Points.Name = "Points"
local success, currentExperience = pcall(function()
return experienceStore:GetAsync(P.UserId)
end)
if success then
print(currentExperience)
end
print(currentExperience)
end)
while wait(1) do
for _, l in Players:GetPlayers() do
print(l.leaderstats.Points.Value)
l.leaderstats.Points.Value += 1
end
end
Players.PlayerRemoving:Connect(function(P)
local sh = P.leaderstats.Points.Value
print(sh)
local success, errorMessage = pcall(function()
experienceStore:SetAsync(P.UserId, sh)
end)
if not success then
print(errorMessage)
end
end)
There is a while loop ABOVE the playerremoving event, nothing below a while loop runs. So you have to move the removing event above the while loop. Also I recommend using “task.wait” as it’s the new version.
Also you aren’t setting the value to the loaded value that was saved from setasync, you’re just printing it:
local success, currentExperience = pcall(function()
return experienceStore:GetAsync(P.UserId)
end)
if success and currentExperience then
print(currentExperience)
Points.Value = currentExperience
end
Added it so that it sets the value to the getasync value that retrives the saved data.
Its a threading issue. Educate your self on how loops and threads work. To put it simple the while loop keeps the code from running whatever is below it hence it being called a loop.
Code runs top to bottom. Any loops between will prevent code below from being accessible.
@HacTR101213 I know you must be new to coding but I recommend you nail down the fundamentals of coding before jumping straight to more complicated things like data stores. Even many experienced scripters struggle with data stores.
I recommend starting with alvinblox’s beginner series on youtube to nail down the fundamentals, they explained very well (dont watch alvin for anything else though because then he starts spoonfeeding and yapping)
After you finish this series, move on to devking’s advanced series. Every tutorial on devking’s advanced make sure you also refer to the documentation (dont watch devking aswell for anything else though because then he starts spoonfeeding and yapping)
Also good on you for using the documentation instead of a yt tutorial, but I recommend for learning the basics that you do use some tutorials like these.
Once you nail down the basics and devking’s advanced series you can continue to refer from docs/devforum only.
(By the way devking does have some outdated information so make sure to keep yourself updated through checking the documentation/devforum on stuff you learn)