Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Why is data not loading when I use teleport service?

Asked by
lucas4114 607 Moderation Voter
8 years ago

So my loading script works perfectly when I join a game... But when I teleport myself to the same place, still using the same loading script, in the same game, it doesn't work and there are no errors or anything else in the output... :/ This is my loading script:

local KillsData = game:GetService("DataStoreService"):GetDataStore("test44Kills") 
local MoneyData = game:GetService("DataStoreService"):GetDataStore("test44Money")
local GoldData = game:GetService("DataStoreService"):GetDataStore("test44Gold")
local DiamondData = game:GetService("DataStoreService"):GetDataStore("test44Diamonds")
local UnitData = game:GetService("DataStoreService"):GetDataStore("test44Units") 
local BoxData = game:GetService("DataStoreService"):GetDataStore("test44Boxes") 
local BoostData = game:GetService("DataStoreService"):GetDataStore("test44Boosts")
local MessageData = game:GetService("DataStoreService"):GetDataStore("test44Messages")
local BossData = game:GetService("DataStoreService"):GetDataStore("test44Bosses") 
local AchievementsData = game:GetService("DataStoreService"):GetDataStore("test44Achievements")
local HasPlayed = game:GetService("DataStoreService"):GetDataStore("test44Played")

game.Players.PlayerAdded:connect(function(plrjoining)
    wait(0.05)
    local success, message = pcall(function()
        print(plrjoining.Name.." has joined.")
        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)
            data.Diamonds.Value = DiamondData: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
            wait()
            for number4, message in pairs (MessageData:GetAsync(PlayerKey)) do
                local value = game.ServerStorage.messagefolder:Clone()
                value.Name = message["Name"]
                value.Text.Value = message["Text"]
                value.GiveReward.Value = message["GiveReward"]
                value.GiveReward.RewardType.Value = message["RewardType"]
                value.GiveReward.Reward.Value = message["Reward"]["Gold"]
                if message["Reward"]["Units"] ~= {} then
                    for i, v in pairs (message["Reward"]["Units"]) do
                        local unit = Instance.new("StringValue")
                        unit.Name = v
                        unit.Value = v
                        unit.Parent = value.GiveReward.Reward
                    end
                end
                value.Parent = data.Messages
            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 = 0
            data.Gold.Value = 2500
            data.Diamonds.Value = 0
            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
            local messages = { {["Text"]= "Welcome to rts game testing!", ["Name"]="New Player!", ["GiveReward"]="1", ["RewardType"]="Units", ["Reward"]={["Gold"]=0,["Units"]={"Gun AI", "Gun AI"}}}, {["Text"]= "test message", ["Name"]="test", ["GiveReward"]="0", ["RewardType"]="None", ["Reward"]={["Gold"]=0,["Units"]={}}} }
            for number4, message in pairs (messages) do
                local value = game.ServerStorage.messagefolder:Clone()
                value.Name = message["Name"]
                value.Text.Value = message["Text"]
                value.GiveReward.Value = message["GiveReward"]
                value.GiveReward.RewardType.Value = message["RewardType"]
                value.GiveReward.Reward.Value = message["Reward"]["Gold"]
                local messageunits = message["Reward"]["Units"]
                if messageunits ~= {} and messageunits then
                    for i,v in pairs (messageunits) do
                        local unit = Instance.new("StringValue")
                        unit.Name = v
                        unit.Value = v
                        unit.Parent = value.GiveReward.Reward
                    end
                end
                value.Parent = data.Messages
            end
            data.Parent = game.ServerStorage.SAVEDDATA

            local smessages = {}
            for snumber4, smessage in pairs (data.Messages:GetChildren()) do
                local smessagerewardthing = smessage.GiveReward.Reward:GetChildren()
                local smessageunits = {}
                if smessagerewardthing ~= {} then
                    for i,v in pairs (smessagerewardthing) do
                        table.insert(smessageunits, v.Name)
                    end
                end
                table.insert(smessages, {["Text"]=smessage.Text.Value, ["Name"]=smessage.Name, ["GiveReward"]=smessage.GiveReward.Value, ["RewardType"]=smessage.GiveReward.RewardType.Value, ["Reward"]={["Gold"]=smessage.GiveReward.Reward.Value,["Units"]= smessageunits}})
            end
            MessageData:SetAsync(PlayerKey, messages)

            BossData:SetAsync(PlayerKey, {"None", "None"})
            AchievementsData:SetAsync(PlayerKey, {"None", "None"})
        end
    end)
    if not success then
        print("An error occurred with loading "..plrjoining.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)

How do I fix this???????

0
Use updateAsync, not setAsync GoldenPhysics 474 — 8y
0
Also. COMMENT lines to explain what it's doing. You'll get help much faster that way. GoldenPhysics 474 — 8y
0
Why UpdateAsync? lucas4114 607 — 8y
0
If you use SetAsync, then get the value using GetAsync, GetAsync might get cached date, old data. UpdateAsync will make sure the value is modified by all. GoldenPhysics 474 — 8y
View all comments (4 more)
0
If you have three SetAsync at the same time, (let's assume the value is 3) and one does val+2, one does val+5, and one does val-3, then the result might be 5, 8, or 0. If they were updateAsync,the result would be 7. GoldenPhysics 474 — 8y
0
So UpdateAsync is like SetAsync but faster right? lucas4114 607 — 8y
0
PlayerAdded for some reason does not run when a player teleports. You're meant to use LocalPlayerArrivedFromTeleport, but that doesn't work right now. TheDeadlyPanther 2460 — 8y
0
So it I cant do anything to fix it? And if it did work it would be game:GetService('TeleportService').LocalPlayerArrivedFromTeleport:connect(function()) --loading stuff end ? lucas4114 607 — 8y

1 answer

Log in to vote
0
Answered by 8 years ago

The main error is the requests you send by creating a lot of USELESS datastores, you should use one datastore called maybe "gamenameDataStore" and create variables from there!

Ad

Answer this question