Issues with datastore

just do

local CurXP = game.Players:GetChildren():WaitForChild("PlayerGui").ExpBarUp.CurrentExp

1 Like

Actually, I have another question.
The script is located right now in ServerScriptService, shouldn’t it be in another place? Maybe StarterPlayerScripts?

You can’t save in local scripts, also UpdateAsync should be used when you want to see the old data.

You can’t do that, there isn’t any function named WaitForChild in tables.

This is the script I am using now, located in ServerScriptsService:

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function()
		local CurrentExp = Player:WaitForChild("PlayerGui"):WaitForChild("EXPBarUP").CurrentExp
		local BlueSize = Player:WaitForChild("PlayerGui"):WaitForChild("EXPBarUP").Frame
		local GetSync = AllDataStores:GetAsync(Player.UserId)
	
		if GetSync then
			print(GetSync[1])
			print(GetSync[2])
			CurrentExp = GetSync[1]
			BlueSize.Size = UDim2.new(GetSync[2], BlueSize.Size.X.Offset, BlueSize.Size.Y.Scale, BlueSize.Size.Y.Offset)		
		end
	end)
end)

game.Players.PlayerRemoving:Connect(function(Player)
    local CurrentExp = Player.PlayerGui.EXPBarUP.CurrentExp
    local BlueSize = Player.PlayerGui.EXPBarUP.Frame.Size.X.Scale
    local TransferValues = {CurrentExp.Value, BlueSize}
    
    print(TransferValues[1])
    print(TransferValues[2])
    
    AllDataStores:SetAsync(Player.UserId, TransferValues)
end)

And I am getting a brand new error:
“PlayerGui is not a vaild member of Player”.
Why?

Simply use :WaitForChild(“PlayerGui”) At the PlayerRemoving Event. If that isn’t working, maybe when the player leaves PlayerGui is destroyed and you should put the values in another place like ServerStorage

1 Like

I can just make a new datastore script. Mine is simpler than what’s happening on this post. Just let me have a minute

1 Like

His datastore script is good and it isn’t having any problem.

1 Like

Oh. Well I guess I’ll make this for personal use.

1 Like

Okay, I never thought about that…
So I have 2 questions now.
I want to save both the size of the frame and the numbervalue value, so:

  • How do I access the size of the frame in a local script? I was told that RemoteEvents are not safe, and not recommended for datastores.
  • Where should I put the CurrentExp instead of PlayerGui? I need it to be accessed by both client and server. ReplicatedStorage maybe?

ReplicatedStorage isn’t the most safe place, maybe inside a folder in the player? Also, you can get the size of the frame from Scripts if you are setting it using scripts.

1 Like

I set it using local scripts…

I have skipped this by a margin but I can see that you are confused on how to create a safe method of saving. I recommend How to use DataStore2 - Data Store caching and data loss prevention as a great alternative to making your own.

Also by safe, you need to make the data you are sending read only. You can either have a remote function invoke the server to return the player data it specifies, or do something lazy like placing the data into a string value for all to see.

I don’t know what you mean by remote events not being ‘safe’. They are as safe as you make them, just have the server check to make sure what you are doing should be done. This is contextual and every action will have different checks.

1 Like
game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function()
		wait()
		local GetSync = AllDataStores:GetAsync("String"..Player.UserId)
		print(GetSync[1])
		CurrentExp.Value = GetSync[1]
	end)
end)

game.Players.PlayerRemoving:Connect(function(Player)
    local TransferValues = {CurrentExp.Value}
    
    print(TransferValues[1])
    
    AllDataStores:SetAsync("String"..Player.UserId, TransferValues)
end)

Keeps getting the error:
[ServerScriptService.DataStoreRemote:31: attempt to index nil with number]

CurrentExp is in replicatedstorage.
Any idea?

Can you show me the error line? Not only the error and the number?

game.Players.PlayerAdded:Connect(function(Player)

wait()

local GetSync = AllDataStores:GetAsync("String"..Player.UserId)

–ERROR HERE print(GetSync[1])

CurrentExp.Value = GetSync[1]

end)

This means GetAsync is nil, add a if GetAsync then

I tried… I tried doing literally everything, I tried if GetAsync == nil, if GetAsync, I tried making a big PCall to make sure, but literally nothing. No matter what I try it’s the same result.

Simply try:

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function()
		wait()
		local succes,GetSync = pcall(AllDataStores.GetAsync,"String"..Player.UserId)
        if typeof(GetAsync) == "table" and succes then
		print(GetSync[1])
		CurrentExp.Value = GetSync[1]
        end
	end)
end)

game.Players.PlayerRemoving:Connect(function(Player)
    local TransferValues = {CurrentExp.Value}
    
    print(TransferValues[1])
    
    AllDataStores:SetAsync("String"..Player.UserId, TransferValues)
end)

Thats because you did not put it inside a variable

and the .Value = YourStored