Assigning Folders On Join - help still needed

  1. What do you want to achieve? Users will join the game and get a folder given to them called “Gender” This will be a data store too. However I thought I could just re-purpose my leaderboard folder giver but I was very wrong.

  2. What is the issue?

  3. What solutions have you tried so far? Browsed devforum, nothing useful.

local DSS = game:GetService('DataStoreService')
local PD = DSS:GetDataStore('PlayerData')

local function onPlayerJoin(player)

	local Gender = Instance.new("Folder")
	Gender.Name = "Gender"
	Gender.Parent = Gender



	local Gender = Instance.new("IntValue")
	Gender.Name = "Gender"
	Gender.Parent = Gender



	local playerUserID = 'Player_'..player.UserId
	local data = PD:GetAsync(playerUserID)
	if data then 
		Gender.Value = data['Gender']
	else
		Gender.Value = 0
	end

end

local function create_table(player)
	local player_stats = {}
	for _, stat in pairs(player.Gender:GetChildren()) do
		player_stats[stat.Name] = stat.Value
	end
	return player_stats
end

local function onPlayerExit(player)
	local player_stats = create_table(player)
	local success, err = pcall(function()
		local playerUserId = 'Player_'..player.UserId
		PD:setAsync(playerUserId, player_stats)

	end)
	if not success then
		warn('Could not save data')
	end
end

game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)

Did you mean

Gender.Parent = player

You tried to parent the folder to itself.

4 Likes

Changing it lead to:


Line 15:

		Gender.Value = data['Gender']

Script is now:

local DSS = game:GetService('DataStoreService')
local PD = DSS:GetDataStore('PlayerData')

local function onPlayerJoin(player)

	local Gender = Instance.new("Folder")
	Gender.Name = "Gender"
	Gender.Parent = player



	local playerUserID = 'Player_'..player.UserId
	local data = PD:GetAsync(playerUserID)
	if data then 
		Gender.Value = data['Gender']
	else
		Gender.Value = 0
	end

end

local function create_table(player)
	local player_stats = {}
	for _, stat in pairs(player.Gender:GetChildren()) do
		player_stats[stat.Name] = stat.Value
	end
	return player_stats
end

local function onPlayerExit(player)
	local player_stats = create_table(player)
	local success, err = pcall(function()
		local playerUserId = 'Player_'..player.UserId
		PD:setAsync(playerUserId, player_stats)

	end)
	if not success then
		warn('Could not save data')
	end
end

game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)

Folder has no Value property. Maybe you meant it to be an IntValue?

Yeah I wanted it to be an int value so 1 = male 2 = female 3 = other (another code handles that)
How would I make it an IntValue?

local DSS = game:GetService('DataStoreService')
local PD = DSS:GetDataStore('PlayerData')

local function onPlayerJoin(player)

	local GenderFolder = Instance.new("Folder")
	GenderFolder.Name = "Gender"
	GenderFolder.Parent = player

	local Gender = Instance.new("IntValue")
	Gender.Name = "Gender"
	Gender.Parent = GenderFolder

	local playerUserID = 'Player_'..player.UserId
	local data = PD:GetAsync(playerUserID)
	if data then 
		Gender.Value = data['Gender']
	else
		Gender.Value = 0
	end
end

local function create_table(player)
	local player_stats = {}
	for _, stat in pairs(player.Gender:GetChildren()) do
		player_stats[stat.Name] = stat.Value
	end
	return player_stats
end

local function onPlayerExit(player)
	local player_stats = create_table(player)
	local success, err = pcall(function()
		local playerUserId = 'Player_'..player.UserId
		PD:setAsync(playerUserId, player_stats)
	end)
	if not success then
		warn('Could not save data')
	end
end

game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)

Try this.
This is basically your first code, I just changed the var Parent and var Name.

I think Gender should be a “String value” and not an “IntValue.”
You didn’t make a leaderboard and just put it as Gender. Instead you could do:

local playerUserID = 'Player_'..player.UserId
local data = PD:GetAsync(playerUserID)

local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player

local Gender = Instance.new("StrValue")
Gender.Name = "Gender"
Gender.Parent = leaderstats
if data then...

Something like that and then you can access this by another script:

game.Players.leaderstats.Gender

Hope it helps.

Also it doesn’t have to be in leaderstats. Maybe they don’t want other players to see someone’s gender.

1 Like

Using this had errors…

	local GenderFolder = Instance.new("Folder")
	Gender.Name = "Gender"
	Gender.Parent = player

So to counter that I changed it to:

	local GenderFolder = Instance.new("Folder")
	GenderFolder.Name = "Gender"
	GenderFolder.Parent = player
  • Testing it now
1 Like

image - Solution found, thank you.

Yeah, completly forgot to change these names :smile: