-
What do you want to achieve?
I’m simply trying to create a data storage system for my game. I’ve tried many different solutions, however, none of them appear to be working. -
What is the issue?
The issue is that I can’t find the problem with the code. I was hoping some of you could take a look at it and tell me what I’m doing wrong. -
What solutions have you tried so far?
I’ve looked for solutions everywhere however none of them appear to be working. I’m convinced the problem has to do with my code (of course it is). I’ve already tried changing where the data was being stored (from Lighting to ReplicatedStorage), but still no luck.
In order to save the Data, I have created 2 scripts, one for creating the data, and one for saving it. Take a look, please.
– Here is the first script that creates the data (This is only a portion of the script, the parts I thought were necessary. If you need to see more of the script let me know.) –
game.Players.PlayerAdded:connect(function(plr)
--Create Leaderstats folder--
local Leaderstats = Instance.new("Folder", game.ReplicatedStorage)
Leaderstats.Name = plr.Name
local Leaderstats = game.ReplicatedStorage:WaitForChild(plr.Name)
--Create values inside of folder--
local Points = Instance.new("NumberValue", Leaderstats)
Points.Name = ("Points")
Points.Value = 0
local Level = Instance.new("NumberValue", Leaderstats)
Level.Name = ("Level")
Level.Value = 1
local UnlockKeys = Instance.new("IntValue", Leaderstats)
UnlockKeys.Name = ("UnlockKeys")
UnlockKeys.Value = 0
local TotalMissionsCompleted = Instance.new("IntValue", Leaderstats)
TotalMissionsCompleted.Name = ("TotalMissionsCompleted")
TotalMissionsCompleted.Value = 0
end)
– Here is the second script that saves the data to DataStorage (This one is the WHOLE script.)–
-- Script will be used to save player's data --
local DataStorage = game:GetService("DataStoreService"):GetDataStore("SaveData")
game.Players.PlayerAdded:Connect(function(Player)
wait(5)
local PlayerId = ("Player-".. Player.UserId)
local SaveLevel = game.ReplicatedStorage:WaitForChild(Player.Name):WaitForChild("Level")
local SavePoints = game.ReplicatedStorage:WaitForChild(Player.Name):WaitForChild("Points")
local SaveMissionsCompleted = game.ReplicatedStorage:WaitForChild(Player.Name):WaitForChild("TotalMissionsCompleted")
local SaveUnlockKeys = game.ReplicatedStorage:WaitForChild(Player.Name):WaitForChild("UnlockKeys")
local GetData = DataStorage:GetAsync(PlayerId)
if GetData then
SaveLevel.Value = GetData[1]
SavePoints.Value = GetData[2]
SaveMissionsCompleted.Value = GetData[3]
SaveUnlockKeys.Value = GetData[4]
else
local Savings = {SaveLevel.Value, SavePoints.Value, SaveMissionsCompleted.Value, SaveUnlockKeys.Value}
DataStorage:GetAsync(PlayerId, Savings)
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
DataStorage:SetAsync("Player-".. Player.UserId, {game.ReplicatedStorage:WaitForChild(Player.Name):WaitForChild("Level").Value,game.ReplicatedStorage:WaitForChild(Player.Name):WaitForChild("Points").Value,game.ReplicatedStorage:WaitForChild(Player.Name):WaitForChild("TotalMissionsCompleted").Value,game.ReplicatedStorage:WaitForChild(Player.Name):WaitForChild("UnlockKeys").Value})
end)
Thank you so much to anyone who helps.
- Zipps