在过去的几天里,我一直被困在数据没有保存的情况下,我的代码没有给出任何错误,而且我可以看到它应该有效。这是代码:
local DataStore = game:GetService("DataStoreService"):GetDataStore("PlayerStats001")
-- Function to make the leaderstats
function onPlayerJoin(player)
local Leaderstats = Instance.new("Folder", player)
Leaderstats.Name = ("leaderstats")
local leadermoney = Instance.new("IntValue", Leaderstats)
leadermoney.Name = ("Money")
local leaderrescues = Instance.new("IntValue", Leaderstats)
leaderrescues.Name = ("Rescues")
local key = "player-" .. player.userId
local savePoints = {}
savePoints = DataStore:GetAsync(key)
if savePoints then
print("Old leaderstats")
-- Has been to game before, save format = {money, rescues}
leadermoney.Value = savePoints[1]
leaderrescues.Value = savePoints[2]
else
print("New leaderstats")
leadermoney.Value = 25000
leaderrescues.Value = 0
local valuesToSave = {leadermoney.Value, leaderrescues.Value}
DataStore:SetAsync(key, valuesToSave)
end
end
-- Saves player data
function save(player)
print("Started save")
local key = "player-" .. player.userId
local valuesToSave = {player.leaderstats:FindFirstChild("Money").Value , player.leaderstats:FindFirstChild("Rescues").Value}
DataStore:SetAsync(key, valuesToSave)
print("Finished save", valuesToSave[1], valuesToSave[2])
end
function playerLeaves(player)
save()
end
-- Runs the save function if a client requests a save
game.Lighting.RemoteEvents.Save.OnServerEvent:Connect(save)
-- Runs the playerLeaves function if a player leaves
game.Players.PlayerRemoving:Connect(save)
-- Runs the onPlayerJoin function when a player joins
game.Players.PlayerAdded:Connect(onPlayerJoin)
当我运行它时(我在启动器GUI中有一个本地脚本可以工作):
function autoSaveRequester()
print("Started auto save function.")
while wait(15) do
print("Finnished wait")
game.Lighting.RemoteEvents.Save:FireServer()
end
end
spawn(autoSaveRequester)
它输出的值与我更改之前的值相同,而不是新值,任何帮助都表示赞赏。
由于这条线,你的代码无法正常工作;
game.Players.PlayerRemoving:Connect(save)
您直接跳转到保存功能,而不是PlayerRemoving,也没有指定播放器。
将该行更新为:
game.Players.PlayerRemoving:Connect(playerLeaves)
如果这有帮助,请确保upvote!如果这回答了你的问题,请不要忘记按下勾号!
如果您还有其他需要,请在下方发表评论
罗斯。