My saving and load both work fine in game, but when I teleported to a different place in my game it doesn't work at all... Both places use the exact same saving and loading scripts why don't they work when I use TeleportService? Loading script:
wait(1)
local KillsData = game:GetService("DataStoreService"):GetDataStore("test21Kills")
local MoneyData = game:GetService("DataStoreService"):GetDataStore("test21Money")
local GoldData = game:GetService("DataStoreService"):GetDataStore("test21Gold")
local UnitData = game:GetService("DataStoreService"):GetDataStore("test21Units")
local BoxData = game:GetService("DataStoreService"):GetDataStore("test21Boxes")
local BoostData = game:GetService("DataStoreService"):GetDataStore("test21Boosts")
local HasPlayed = game:GetService("DataStoreService"):GetDataStore("test21Played")
game.Players.PlayerAdded:connect(function(plrjoining)
wait(0.05)
local success, message = pcall(function()
local PlayerKey = "user_"..plrjoining.userId
local plrnew = false
if HasPlayed:GetAsync(PlayerKey) == nil then
print(plrjoining.Name.." is a new player!")
plrnew = true
local PlayerKey = "user_"..plrjoining.userId
HasPlayed:SetAsync(PlayerKey, true)
end
if plrnew == false then
print(plrjoining.Name.." is loading.")
local PlayerKey = "user_"..plrjoining.userId
local data = game.ServerStorage.savefolder:Clone()
data.Name = plrjoining.Name
data.Kills.Value = KillsData:GetAsync(PlayerKey)
data.Money.Value = MoneyData:GetAsync(PlayerKey)
data.Gold.Value = GoldData:GetAsync(PlayerKey)
for number, unit in pairs (UnitData:GetAsync(PlayerKey)) do
local value = Instance.new("StringValue")
value.Name = unit
value.Value = unit
value.Parent = data.Units
end
wait()
for number2, box in pairs (BoxData:GetAsync(PlayerKey)) do
local value = Instance.new("StringValue")
value.Name = box
value.Value = box
value.Parent = data.Boxes
end
wait()
for number3, boost in pairs (BoostData:GetAsync(PlayerKey)) do
local value = Instance.new("StringValue")
value.Name = boost
value.Value = boost
value.Parent = data.Boosts
end
data.Parent = game.ServerStorage.SAVEDDATA
print(plrjoining.Name.." has loaded, Money: "..MoneyData:GetAsync(PlayerKey).." Kills: "..KillsData:GetAsync(PlayerKey).." Units: "..table.concat(UnitData:GetAsync(PlayerKey), " ").." Boxes: "..table.concat(BoxData:GetAsync(PlayerKey), " "))
elseif plrnew == true then
print(plrjoining.Name.." is loading.")
local PlayerKey = "user_"..plrjoining.userId
local data = game.ServerStorage.savefolder:Clone()
data.Name = plrjoining.Name
data.Kills.Value = 0
data.Money.Value = 500
data.Gold.Value = 10
local units = {"Gun AI", "Tank AI"}
for number, unit in pairs (units) do
local value = Instance.new("StringValue")
value.Name = unit
value.Value = unit
value.Parent = data.Units
end
local boxes = {"Box #1"}
for number2, box in pairs (boxes) do
local value = Instance.new("StringValue")
value.Name = box
value.Value = box
value.Parent = data.Boxes
end
local boosts = {"Boost #1"}
for number3, boost in pairs (boosts) do
local value = Instance.new("StringValue")
value.Name = boost
value.Value = boost
value.Parent = data.Boosts
end
data.Parent = game.ServerStorage.SAVEDDATA
end
end)
if not success then
print("An error occurred with loading "..plrjoining.Name.."'s data: "..message)
end
end)
Saving script:
wait(1)
local KillsData = game:GetService("DataStoreService"):GetDataStore("test21Kills")
local MoneyData = game:GetService("DataStoreService"):GetDataStore("test21Money")
local GoldData = game:GetService("DataStoreService"):GetDataStore("test21Gold")
local UnitData = game:GetService("DataStoreService"):GetDataStore("test21Units")
local BoxData = game:GetService("DataStoreService"):GetDataStore("test21Boxes")
local BoostData = game:GetService("DataStoreService"):GetDataStore("test21Boosts")
local HasPlayed = game:GetService("DataStoreService"):GetDataStore("test21Played")
game.Players.PlayerRemoving:connect(function(plrleaving)
local success, message = pcall(function()
print(plrleaving.Name.." is leaving.")
local PlayerKey = "user_"..plrleaving.userId
local data = game.ServerStorage.SAVEDDATA[plrleaving.Name]
local units = {}
for number, unit in pairs (data.Units:GetChildren()) do
table.insert(units, unit.Value)
end
UnitData:SetAsync(PlayerKey, units)
local boxes = {}
for number2, box in pairs (data.Boxes:GetChildren()) do
table.insert(boxes, box.Value)
end
BoxData:SetAsync(PlayerKey, boxes)
local boosts = {}
for number3, boost in pairs (data.Boosts:GetChildren()) do
table.insert(boosts, boost.Value)
end
BoostData:SetAsync(PlayerKey, boosts)
KillsData:SetAsync(PlayerKey, data.Kills.Value)
MoneyData:SetAsync(PlayerKey, data.Money.Value)
GoldData:SetAsync(PlayerKey, data.Gold.Value)
data:Destroy()
print(plrleaving.Name.." left.")
end)
if not success then
print("An error occurred with saving "..plrleaving.Name.."'s data: "..message)
end
end)
Teleport script (In a local script):
script.Parent.MouseButton1Click:connect(function()
local Sound = Instance.new("Sound")
Sound.SoundId = "http://www.roblox.com/asset/?id=156286438"
Sound.Volume = 0.7
Sound.Pitch = 1.2
Sound.Parent = script.Parent
Sound.Name = "Error"
Sound:Play()
game:GetService("Debris"):AddItem(Sound, 5)
game:GetService("TeleportService"):Teleport(358540015)
end)
