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

How do I save data from one place to another in the same game?

Asked by 3 years ago

I've been trying to do this all day but I can't figure it out, does anybody know how I can get it working?

I have a script in the Menu of the game and another in the main game.

Script 1:

local ds = game:GetService("DataStoreService"):GetDataStore("PlayerName")

game.Players.PlayerRemoving:Connect(function(plr)
    local f = Instance.new("Folder")
    f.Name = "leaderstats"
    f.Parent = plr

    local nameVal = Instance.new("StringValue")
    nameVal.Name = "NameValue"
    nameVal.Parent = f
    nameVal.Value = plr.PlayerGui.ScreenGui.Frame.TextBox.Text

    game.Players.PlayerRemoving:Connect(function(player)
        if player.UserId == plr.UserId then
            ds:SetAsync(player.UserId, player.leaderstats.NameValue.Value)
        end
    end)
end)

Script 2:

local ds = game:GetService("DataStoreService"):GetDataStore("PlayerName")
local data

game.Players.PlayerAdded:Connect(function(plr)
    local f = Instance.new("Folder")
    f.Name = "leaderstats"
    f.Parent = plr

    local nameVal = Instance.new("StringValue")
    nameVal.Name = "NameValue"
    nameVal.Parent = f

    data = ds:GetAsync(plr.UserId)
    plr.leaderstats.NameValue.Value = data
end)
0
I fixed it by using teleport data instead of using datastores Scryptol 2 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

DataStores DON'T sav strings try using the player's user ID

Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

Make sure your places are connected to the same universe. DataStores cannot be accessed from different universes YET.

Also, your Script 1. There is a PlayerRemoving event inside a PlayerRemoving event. I think you meant to do PlayerAdded?

PlayerAdded and PlayerRemoving should be separate, not inside of each other. If you kept it like Script 1, you could end up having too much connected signals, resulting in lag when a player leaves.

Maybe consider looking into sending teleportData, assuming nameVal isn't important: https://developer.roblox.com/en-us/api-reference/function/TeleportService/Teleport

Otherwise, it should look something like this:

local ds = game:GetService("DataStoreService"):GetDataStore("PlayerName")

game.Players.PlayerAdded:Connect(function(plr)
    local f = Instance.new("Folder")
    f.Name = "leaderstats"
    f.Parent = plr

    local nameVal = Instance.new("StringValue")
    nameVal.Name = "NameValue"
    nameVal.Parent = f
    nameVal.Value = plr.PlayerGui.ScreenGui.Frame.TextBox.Text
    -- you want to somehow update this
    -- whether it is through TextBox.Changed, TextBox.FocusLost, or loops.
    -- The update MUST go to the server, so use RemoteEvents
end)

game.Players.PlayerRemoving:Connect(function(player)
    ds:SetAsync(player.UserId, player.leaderstats.NameValue.Value)
end)

Answer this question