Datastore dont work with ipairs help me pls

I write this but it dont work. Help pls

local Players = game.Players
local DataStoreService = game:GetService("DataStoreService")

local Info1 = DataStoreService:GetDataStore("Info1")
local Info2 = DataStoreService:GetDataStore("Info2")
local Info3 = DataStoreService:GetDataStore("Info3")

local function savePlayerData(plr)
	print("Save")
	for _, button in pairs(script.Parent:GetChildren()) do 
		if button:IsA("TextButton")  then
		print("Here script stopping. Helpppp")
			for _, frame in pairs(button:GetChildren()) do
				if frame:IsA("Frame") then
					local boughtLevels = frame:WaitForChild("boughtLevels")
					local BuyLvlPrice = frame:WaitForChild("BuyLvlPrice")
					local GiveMoneyReal = frame:WaitForChild("GiveMoneyReal")
					
					Info1:SetAsync(plr.UserId, boughtLevels.Value)
					Info2:SetAsync(plr.UserId,BuyLvlPrice.Value)
					Info3:SetAsync(plr.UserId,GiveMoneyReal.Value)
					print("Saved")
				end
			end
		end
	end
end

local function loadPlayerData(plr)
	print("Load")
	for _, button in pairs(script.Parent:GetChildren()) do 
		print("Load1")
		if button:IsA("TextButton")  then
		print("Here script stopping. Helpppp")
			for _, frame in pairs(button:GetChildren()) do
				print("Load2")
				if frame:IsA("Frame") then
					print("Load3")
					local boughtLevels = frame:WaitForChild("boughtLevels")
					local BuyLvlPrice = frame:WaitForChild("BuyLvlPrice")
					local GiveMoneyReal = frame:WaitForChild("GiveMoneyReal")

					boughtLevels.Value = Info1:GetAsync(plr.UserId) or 0
					BuyLvlPrice.Value = Info2:GetAsync(plr.UserId) or 0
					GiveMoneyReal.Value = Info3:GetAsync(plr.UserId) or 0
					print("Loaded")
				end
			end
		end
	end
end
6 Likes

You used pairs() instead of ipairs() so I guess you need to switch that.

6 Likes

I think they both almost the same, it’s a little bit different, but just a little cutely bit

4 Likes

There is nothing wrong with pairs() and ipairs(). You should note that ipairs() is more for arrays (to efficiently iterate in order) and pairs() is more for dictionaries (ipairs doesn’t work for dictionaries) though. However, the problem with your code is most likely that it doesn’t halt for your buttons to load in when the client loads up. You could use the ContentProvider service to Preload your buttons in before loading in the values.

Edit: Also may I ask where your DataStore script is parented? Looking through the path structure of your variables, it seems like it’s in one of your GUIs which is not where it should be and can cause more issues.

2 Likes

Hello there, there was another post with the same problem by the OP. He marked switching to ipairs() fixed his problem, so no need to help anymore.

3 Likes

Another post? It’s a little confusing because it doesn’t seem like they found a solution through this topic. Also switching pairs() to ipairs() I doubt is going to fully fix the issue. That’s why I suggested looking into ContentProvider and maybe relocating the DataStore script?

3 Likes

Its dont helped. Idk how to fix. I thought it was supposed to help when I was not at home, so I marked it with a solution

2 Likes

If I understood correctly what you meant, I can just write task.wait(3) in my script and everything will be fine. It didn’t work.

3 Likes

No, task.wait() and ContentProvider are separate things. I would consider reading into the documentation I linked above when you find the time. Using task.wait() is a bad practice when it comes to stuff like this and really shouldn’t be used like that.

Also what type of script is this and where is it located? I feel like that may be springing on some issues too.

3 Likes

What. I saw you wrote a move script. Previously, he was in the server script service. in workspace, he works almost with a bang. Before that, he stopped at print(“Load1”). While in ServerScriptService, it stopped working on if frame:IsA(“Frame”) then. I’ll read the documentation now, as you advised

1 Like

Ok, I think I might know multiple issues then. First make sure the script is in ServerScriptService. All of your server scripts that handle data should really be located there. Next, ScreenGui is not the same as the PlayerGui. Changing something in the ScreenGui won’t change anything for the player.

You are going to have to locate the player (PlayerAdded event) look through the PlayerGui path and then it should be just like what you have in your ScreenGui. The ScreenGui in Studio is more of a workplace which clones all of the ui over into the player’s client.

should be as simple as:

function loadPlayerData(plr) -- again you'll most likely need to use ContentProvider to preload all of the player's assets
    local PlayerGui = plr.PlayerGui -- locate the PlayerGui
    for _, button in ipairs(PlayerGui[whatever path you have]:GetChildren()) do
        -- code here
    end
end
2 Likes
local function savePlayerData(plr)
	local PlayerGui = plr.PlayerGui -- PlayerGui is not a valid member of My Player
	-- What I can do? 
	for _, button in pairs(PlayerGui:WaitForChild("Game").Profit.Markets:GetChildren()) do 
		if button:IsA("TextButton")  then
			for _, frame in pairs(button:GetChildren()) do
				if frame:IsA("Frame") then
					local boughtLevels = frame:WaitForChild("boughtLevels")
					local BuyLvlPrice = frame:WaitForChild("BuyLvlPrice")
					local GiveMoneyReal = frame:WaitForChild("GiveMoneyReal")
					local GiveMoneyInHour = frame:WaitForChild("GiveMoneyInHour")
					Info1:SetAsync(plr.UserId, boughtLevels.Value)
					Info2:SetAsync(plr.UserId,BuyLvlPrice.Value)
					Info3:SetAsync(plr.UserId,GiveMoneyReal.Value)
					Info4:SetAsync(plr.UserId,GiveMoneyInHour.Value)
					print("Saved")
				end
			end
		end
	end
end

Its didnt work because As I understand it, the script simply does not have time to get PlayerGui when the player exits, even though PlayerRemoving Occurs before the player exits

1 Like

Let’s focus on the player entering the game first and we can come back to saving the data, because otherwise this might get disorganized. Since the player just loads in you will need to call a WaitForChild() on the PlayerGui, so the code yields for the instance to load in (again you’ll most likely need ContentProvider to preload the player’s ui).

Before we go any further I just want to make something clear, all of your textbuttons and frames are a descendant of the StarterGui correct? I overread your mention about it working in workspace, so I wanted to make sure this wasn’t something that was meant to be global.

Also sorry I meant StarterGui when I said ScreenGui (ScreenGui is the gui instance you add that resides in startergui and playergui).

2 Likes

I’ve added a couple of edits to my script. He is currently working in the server script service. And yes, all of my textbuttons and frames are a descendant of the correct

2 Likes

Ok, I actually have another question which might make a lot of things easier (you could probably avoid using ContentProvider knowing this information). Could you parent those values in a folder located inside the player and then in a local script under whatever it is you need to change, manipulate it?

I don’t know exactly why the values are located inside the player’s ui in the first place, I feel like that might make it harder to locate them if necessary for other scripts (also feels like a problem for any occurrences of people hacking).

3 Likes

I cant load video here idk why. Can we go to discord so that I can send you a video? Explaining the hierarchy is a bit difficult

2 Likes

If the video is too big then it won’t upload here, you might have to compress it or chop it into multiple short videos. I also would rather not resort to discord because the point of you making this post is to document for yourself and others what went wrong and how to fix the issue.

That also being said, I don’t want to accidentally flood this topic up, so it might be beneficial to research through datastore documents and tutorials online. I think the main issue here is most likely that you’ve put values inside of the Player UI when you really should be using Instance.new() to create a new folder within the player that can hold their data (and then use local scripts in your ui to manipulate anything you need to). The method you’ve got going right now seems like it has many issues within itself.

replace each pairs and ipairs with just in

eg. bad code
for i, v in pairs(children) do

good code
for i, v in children do

I found the easiest solution in creating a “SaveData button”. But it doesn’t work! I mean, it turns out - the whole script doesn’t work!