Im trying to save data with this script but for some reason it wont save when I change it
local DataStoreService = game:GetService("DataStoreService")
local playerDataStore = DataStoreService:GetDataStore("PlayerData")
local players = game:GetService("Players")
local CurrentData = {}
local data = game.ServerStorage.Data
function playerAddedEvent(player)
data.Parent = player
local success = nil
local playerdata = nil
local attempt = 1
repeat
success, playerdata = pcall(function()
return playerDataStore:GetAsync(player.UserId)
end)
attempt += 1
if not success then
warn(playerdata)
task.wait(2)
end
until success or attempt == 5
if success then
print("Connected to data store correctly")
if not playerdata then
print("Assigning default data")
playerdata = {
Money = data.Money.Value,
Rank = data.Rank.Value,
}
end
CurrentData[player.UserId] = playerdata
print("CurrentData updated for player "..player.UserId..": "..tostring(playerdata))
end
end
function playerLeft(player)
if CurrentData[player.UserId] then
local success = nil
local errormsg = nil
local attempt = 1
repeat
success, errormsg = pcall(function()
playerDataStore:SetAsync(player.UserId, CurrentData[player.UserId])
end)
attempt += 1
if not success then
warn(errormsg)
task.wait(2)
end
until success or attempt == 5
if success then
print("Data saved correctly for player "..player.UserId)
else
warn("Unable to save data for player "..player.UserId..": "..errormsg)
player:Kick("Unable to get player data")
end
end
end
players.PlayerRemoving:Connect(playerLeft)
players.PlayerAdded:Connect(playerAddedEvent)
playerDataStore is not assigned
Im confused where is it not assigned
i am wrong sorry but
why
data.Parent = player
is not
data:Clone().Parent = player
?
Ok let me do that thank you, do you know anything else I need to change
It’s too complicated for nothing
local DataStoreService = game:GetService("DataStoreService")
local playerDataStore = DataStoreService:GetDataStore("PlayerData")
local players = game:GetService("Players")
local CurrentData = {}
local data = game.ServerStorage.Data
function playerAddedEvent(player)
data:Clone().Parent = player
local playerdata = playerDataStore:GetAsync(player.UserId) or {
Money = data.Money.Value,
Rank = data.Rank.Value,
}
CurrentData[player.UserId] = playerdata
print("CurrentData updated for player "..player.UserId..": "..tostring(playerdata))
end
function playerLeft(player)
playerDataStore:SetAsync(player.UserId, CurrentData[player.UserId])
end
players.PlayerRemoving:Connect(playerLeft)
players.PlayerAdded:Connect(playerAddedEvent)
Weird thing is that everything prints out properly
So it’s works the script that i give you?
No currently it still wont save with the script you sent
Can you try this script?
local DataStoreService = game:GetService("DataStoreService")
local playerDataStore = DataStoreService:GetDataStore("PlayerData")
local players = game:GetService("Players")
local CurrentData = {}
local defaultMoney = 0
local defaultRank = 0
function playerAddedEvent(player)
local playerdata = playerDataStore:GetAsync(player.UserId) or {
Money = defaultMoney,
Rank = defaultRank,
}
CurrentData[player.UserId] = playerdata
print("CurrentData updated for player "..player.UserId..": "..tostring(playerdata))
end
function playerLeft(player)
playerDataStore:SetAsync(player.UserId, CurrentData[player.UserId] )
end
players.PlayerRemoving:Connect(playerLeft)
players.PlayerAdded:Connect(playerAddedEvent)