Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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)

1 comment

0

I fixed it by using teleport data instead of using datastores

2 answers

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

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.