Okay so I watched and read a lot about DataStore2 but I have a feeling im still doing something wrong…
People complain about loosing their names in my roleplay game so could anyone help me and check if anything is wrong with my code if yes i would really appreciate it .
Code:
local Players = game:GetService(“Players”)
local ServerScriptService = game:GetService(“ServerScriptService”)
local DataStore2 = require(ServerScriptService.DataStorage.DataStore2Module)
DataStore2.Combine(“Data”, “Coins”, “FirstName”, “LastName”)
local Names = {“Young”,“Oliva”}
local NormalLastNames = {“Kim”,“Lazer”}
local RareVariable = {“common”,“common”,“common”,“common”,“common”,“rare”}
local RareNames = {“Joe”,“Bob”,“Beni”}
local DefaultCash = 500
local function RandomLastName()
local LastNameChance = RareVariable[math.random(1, #RareVariable)]
if LastNameChance == “common” then
local LastNameNormal = NormalLastNames[math.random(1, #NormalLastNames)]
return LastNameNormal
elseif LastNameChance == “rare” then
local LastNameRare = RareNames[math.random(1, #RareNames)]
return LastNameRare
end
end
local function RandomFirstName()
local FirstNameRando = Names[math.random(1, #Names)]
return FirstNameRando
end
Players.PlayerAdded:Connect(function(player)
local Cash = Instance.new("IntValue")
Cash.Name = "Cash"
Cash.Parent = StatsFolder
local FirstName = Instance.new("StringValue")
FirstName.Name = "FirstName"
FirstName.Parent = NamesFolder
local LastName = Instance.new("StringValue")
LastName.Name = "LastName"
LastName.Parent = NamesFolder
local coinStore = DataStore2("Coins", player)
local FirstNameStore = DataStore2("FirstName", player)
local LastNameStore = DataStore2("LastName", player)
local function cashUpdate(value)
Cash.Value = coinStore:Get(value)
end
local function FirstNameUpdate(value)
if value == nil or value == "" or value:match("^%s*$") then
local val = RandomFirstName()
FirstName.Value = FirstNameStore:Get(val)
else
FirstName.Value = FirstNameStore:Get(value)
end
end
local function LastNameUpdate(value)
if value == nil or value == "" or value:match("^%s*$") then
local val = RandomLastName()
LastName.Value = LastNameStore:Get(val)
else
LastName.Value = LastNameStore:Get(value)
end
end
cashUpdate(DefaultCash)
FirstNameUpdate(nil)
LastNameUpdate(nil)
coinStore:OnUpdate(cashUpdate)
FirstNameStore:OnUpdate(FirstNameUpdate)
LastNameStore:OnUpdate(LastNameUpdate)
if FirstName.Value == nil or FirstName.Value == "" or FirstName.Value:match("^%s*$") then
local val = RandomFirstName()
FirstName.Value = FirstNameStore:Get(val)
FirstName.Value = val
end
if LastName.Value == nil or LastName.Value == "" or LastName.Value:match("^%s*$") then
local val = RandomLastName()
LastName.Value = LastNameStore:Get(val)
LastName.Value = val
end
StatsFolder.Name = "Stats"
local function SaveAllData()
task.wait()
coinStore:Set(Cash.Value)
FirstNameStore:Set(FirstName.Value)
LastNameStore:Set(LastName.Value)
end
Players.PlayerRemoving:Connect(function(plr)
if plr.Name == player.Name then
SaveAllData()
end
end)
end)