Hello, im new to Lua and am not so good : /, i was trying to add a system that gave points once a second and then saved them so when you rejoined you still had them, but for some reason it didnt work, i tried using a video tutorial and it still didnt work, here is the code:
local DataStorageService = game:GetService("DataStoreService")
local myDataStore = DataStorageService:GetDataStore("myDataStore")
local function onPlayerJoin(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local points = Instance.new("IntValue")
points.Name = "Points"
points.Value = 0
points.Parent = leaderstats
wait(1)
while true do
points.Value = points.Value + 1
wait(1)
end
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(player.UserId.."-points")
end)
if success then
points.Value = data
else
print("there was an error")
warn(errormessage)
end
end
local function onPlayerLeave(player)
local success, errormessage = pcall(function()
myDataStore:SetAsync(player.UserID.."-points",player.leaderstats.points.Value)
end)
if success then
print("successfully stored player data")
else
print("there was an error")
warn(errormessage)
end
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerLeave)
if someone could help me that would be greatly appreciated because i truly have no idea what im doing
As i said im a total noob when it comes to lua or server stuff or whatever so i dont know how the points are being added also where do i add UpdateAsync()
Edit: You did add it from a server script, my bad, sorry about that
Datastores should be working in studio and in game. If it wasn’t working my data wouldn’t be saved.
To reply to your second question, UpdateAsync() is done like this:
local success, err = pcall(function()
pointsDataStore:UpdateAsync(playerKey, function(oldValue)
local newValue = oldValue or 0
newValue = newValue + 50
return newValue
end)
end)
What UpdateAsync() does is that it takes a value of a key from a data store and updates it with a new value. Basically, it accounts for the previous value which SetAsync() does not do. Shold be used instead of SetAsync(), but I can’t control you if you really don’t know how to use it
You can probably input it when you add the points to your stats
When you are using loops the next line of code won’t run until the loop finished, while true do is an infinite loop so the lines below it won’t ever run if you don’t break the loop at any point, you need change the position of the loop and it should work:
local DataStorageService = game:GetService("DataStoreService")
local myDataStore = DataStorageService:GetDataStore("myDataStore")
local function onPlayerJoin(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local points = Instance.new("IntValue")
points.Name = "Points"
points.Value = 0
points.Parent = leaderstats
wait(1)
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(player.UserId.."-points")
end)
if success then
points.Value = data
else
print("there was an error")
warn(errormessage)
end
while true do
points.Value = points.Value + 1
wait(1)
end
end
local function onPlayerLeave(player)
local success, errormessage = pcall(function()
myDataStore:SetAsync(player.UserID.."-points",player.leaderstats.points.Value)
end)
if success then
print("successfully stored player data")
else
print("there was an error")
warn(errormessage)
end
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerLeave)
Also be sure that Studio Access to API Services is enabled in the game settings.