Hello,
I am trying to make a game kind of similar to game dev simulator and I need some help with the data store. As of right now I got the main data working but am unable to save the NPC’s (workers). Can someone help with that?
Here is a screen shot of my problem.
as you can see everything works as supposed to. But as soon as I rejoin the game it does not save the Employees Data.
I have already checked youtube and the dev forums and cant find anything to fix this.
My script is below:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local StarterPlayerEvents = ReplicatedStorage.Events.StarterEmployee
local NewPlayerJack = StarterPlayerEvents.Jack
local NewPlayerEmma = StarterPlayerEvents.Emma
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("test")
local OwnedEmployeesServer = {}
--Employee Data Information--
--[[
[NPC Name] = {
Level,
Salary,
Rarity,
Job Type,
Rank of job (ex. Head Programmer or Programmer)
}
]]
local OwnedEmployees = {
[1] = {
["Name"] = "Jack",
["Salary"] = 100,
["Rarity"] = "Common",
["Type"] = "Programmer",
["Rank"] = "Entry Programmer"
},
[2] = {
["Name"] = "Emma",
["Salary"] = 100,
["Rarity"] = "Common",
["Type"] = "Builder",
["Rank"] = "Entry Builder"
},
}
local function onPlayerJoin(plr)
local Employees = Instance.new("Folder", plr)
Employees.Name = "Employees"
local Data = Instance.new("Folder", plr)
Data.Name = "Data"
local EmployeeTemplate = Instance.new("Folder", plr)
EmployeeTemplate.Name = "EmployeeTemplate"
local Cash = Instance.new("IntValue", Data)
Cash.Name = "Cash"
Cash.Value = 100
local Gems = Instance.new("IntValue", Data)
Gems.Name = "Gems"
Gems.Value = 0
local Rebirths = Instance.new("IntValue", Data)
Rebirths.Name = "Rebirths"
Rebirths.Value = 0
local plrId = "Player_"..plr.UserId
local data = DataStore:GetAsync(plrId)
if data then
Cash.Value = data["Cash"]
Gems.Value = data["Gems"]
OwnedEmployees.Value = data[OwnedEmployees[1]]
OwnedEmployees.Value = data[OwnedEmployees[2]]
Rebirths.Value = data["Rebirths"]
else
Cash.Value = 100
Gems.Value = 0
Rebirths.Value = 0
print("New player:", plr.Name)
end
NewPlayerJack.OnServerEvent:Connect(function()
local Employee = EmployeeTemplate:Clone()
Employee.Name = OwnedEmployees[1].Name
Employee.Parent = Employees
local EmployeeSalery = Instance.new("IntValue", Employee)
EmployeeSalery.Name = "Salary"
EmployeeSalery.Value = OwnedEmployees[1].Salary
local EmployeeRarity = Instance.new("StringValue", Employee)
EmployeeRarity.Name = "Rarity"
EmployeeRarity.Value = OwnedEmployees[1].Rarity
local EmployeeType = Instance.new("StringValue", Employee)
EmployeeType.Name = "Type"
EmployeeType.Value = OwnedEmployees[1].Type
local EmployeeRank = Instance.new("StringValue", Employee)
EmployeeRank.Name = "Rank"
EmployeeRank.Value = OwnedEmployees[1].Rank
end)
NewPlayerEmma.OnServerEvent:Connect(function()
local Employee = EmployeeTemplate:Clone()
Employee.Name = OwnedEmployees[2].Name
Employee.Parent = Employees
local EmployeeSalery = Instance.new("IntValue", Employee)
EmployeeSalery.Name = "Salary"
EmployeeSalery.Value = OwnedEmployees[2].Salary
local EmployeeRarity = Instance.new("StringValue", Employee)
EmployeeRarity.Name = "Rarity"
EmployeeRarity.Value = OwnedEmployees[2].Rarity
local EmployeeType = Instance.new("StringValue", Employee)
EmployeeType.Name = "Type"
EmployeeType.Value = OwnedEmployees[2].Type
local EmployeeRank = Instance.new("StringValue", Employee)
EmployeeRank.Name = "Rank"
EmployeeRank.Value = OwnedEmployees[2].Rank
end)
print(plr.Name, "has joined with loaded data")
end
local function create_table(plr)
local player_data = {}
for _, stat in pairs(plr.Data:GetChildren() and plr.Employees:GetChildren()) do
player_data[stat.Name] = stat
end
return player_data
end
local function onPlayerExit(plr)
local player_data = create_table(plr)
local success, err = pcall(function()
local plrId = "Player_"..plr.UserId
DataStore:SetAsync(plrId, player_data)
end)
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)
Can anybody help with that?
Thanks!