So I have made some changes here and there as none of those suggestions helped me fix the problem.
So I have some new code that I like to share that might be of better help in helping me out.
First of all I have a ServerScript and the first few lines are for the money a new player starts with.
game:WaitForChild("ReplicatedStorage")
vs = game:GetService("ReplicatedStorage"):WaitForChild("VehicleSpawns")
ps = game:GetService("ReplicatedStorage"):WaitForChild("VehiclePurchases")
remove = game:GetService("ReplicatedStorage"):WaitForChild("RemoveCar")
sell = game:GetService("ReplicatedStorage"):WaitForChild("SellCar")
em = game:GetService("ReplicatedStorage"):WaitForChild("EarnMoney")
respawn = game:GetService("ReplicatedStorage"):WaitForChild("Respawn")
rd = game:GetService("ReplicatedStorage"):WaitForChild("ResetData")
data = game:GetService("ReplicatedStorage"):WaitForChild("Load")
ba = game:GetService("ReplicatedStorage"):WaitForChild("BA")
function rd.OnServerInvoke(plr)
if plr:FindFirstChild("Data") and plr:FindFirstChild("Stats") then
for _,child in pairs (plr.Data.Stats:GetChildren()) do
if child.ClassName == "BoolValue" then
if child.Name == "Fiat 500 Purchased" then
child.Value = true
else
child.Value = false
end
end
if child.Name == "Money" or child.Name == "PendingMoney" then
child.Value = 32000
end
end
end
plr:LoadCharacter()
end
function ba.OnServerInvoke(plr)
local hum = nil
for _,child in pairs (plr.Character:GetChildren()) do
if child.Name == "Humanoid" or child.Name == "PhotoMode" then
hum = child
end
if child.ClassName == "Accessory" then
if hum.Sit == true then
if child.Handle:FindFirstChild("BodyBackAttachment") then
child.Handle.Transparency = 1
end
else
if child.Handle:FindFirstChild("BodyBackAttachment") then
child.Handle.Transparency = 0
end
end
end
end
end
function data.OnServerInvoke(player)
player:WaitForChild("Data")
player:WaitForDataReady()
repeat wait() until player:FindFirstChild("Data")
if player.DataReady then
if player:findFirstChild("Data") then
local score = player.Data.Stats:GetChildren()
for i = 1,#score do
local ScoreLoaded = player:LoadNumber(score[i].Name)
wait()
if ScoreLoaded ~= 0 then
score[i].Value = ScoreLoaded
end
end
end
else
if player:FindFirstChild("PlayerGui") and player.PlayerGui:FindFirstChild("Notification") then
player.PlayerGui.Notification:WaitForChild("LocalNotification").Value = "Failed to load data. Please rejoin immediately."
end
end
end
function respawn.OnServerInvoke(plr)
plr:LoadCharacter()
print(plr.Name.." has respawned.")
end
function em.OnServerInvoke(plr)
if plr:FindFirstChild("Data") and plr.Data:FindFirstChild("Stats") and plr.Data.Stats:FindFirstChild("Money") and plr.Data.Stats:FindFirstChild("PendingMoney") then
local M = plr.Data.Stats.Money
M.Value = M.Value + plr.Character.HumanoidRootPart.Velocity.Magnitude / 2.5
wait(0.25)
if plr.Data.Stats.Money.Value > plr.Data.Stats.PendingMoney.Value + 4500 then
plr.Data.Stats.Money.Value = plr.Data.Stats.PendingMoney.Value
end
wait()
plr.Data.Stats.PendingMoney.Value = plr.Data.Stats.Money.Value
end
end
Then I do have another ServerScript that does store all my data
Alpha = true
function onPlayerEntered(newPlayer)
if not newPlayer:FindFirstChild("Data") then
local d = Instance.new("Configuration")
d.Name = "Data"
d.Parent = newPlayer
local stats = Instance.new("Folder")
stats.Name = "Stats"
stats.Parent = d
local cash = Instance.new("IntValue")
cash.Name = "Money"
cash.Parent = stats
cash.Value = 32000
local pcash = Instance.new("IntValue")
pcash.Name = "PendingMoney"
pcash.Parent = stats
pcash.Value = cash.Value
local lang = Instance.new("StringValue")
lang.Parent = stats
lang.Value = "English"
lang.Name = "Language"
local alpha = Instance.new("BoolValue")
alpha.Name = "AlphaPlayer"
alpha.Value = false
if Alpha == true then
alpha.Value = true
end
alpha.Parent = stats
for _,child in pairs (game.Lighting.Vehicles:GetChildren()) do
if stats:FindFirstChild(child.Name.." Purchased") then
else
local I = Instance.new("BoolValue")
I.Parent = stats
I.Name = child.Name.." Purchased"
if child.Name == "Fiat 500" then
I.Value = true
else
I.Value = false
end
end
end
end
end
function leaving(player)
if player.ClassName == "Player" then
if player:FindFirstChild("Data") and player.Data:FindFirstChild("Stats") and player.Data.Stats:FindFirstChild("Money") and player:FindFirstChild("Values") and player.Values:FindFirstChild("Leaving") then
local saving = false
if player.Data.Stats.Money.Value == 32000 then
error("Failed to save "..player.Name.."'s Data")
else
if saving == false then
saving = true
local score = player.Data.Stats:GetChildren()
for i = 1,#score do
player:SaveNumber(score[i].Name,score[i].Value)
end
print("Successfully saved "..player.Name.."'s Data")
saving = false
end
end
end
end
end
game:GetService("Players").PlayerRemoving:connect(leaving)
game:GetService("Players").ChildAdded:connect(onPlayerEntered)
and then there is the make money script which was the first request which gives the player new money.
I would love it if the DataStores the vehicles the player does own and the money the player has and that as soon as the player leaves the game his stats are being saved and he is able to continue with those the first time he joins again.
Atm the vehicles and money is getting wiped and as soon as the player rejoins he starts back with 32k Cash and has no cars anymore.
I really do love the support you guys gave me so far and hopefully you guys are able to help me fix this.
1 comment
Using DataStores should be done on the server side. As this code provided shows no use of it then I'm assuming you have never used it. You can learn here: https://developer.roblox.com/articles/Saving-player-data-using-PlayerDataStore