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

How do I assign each player to a folder?

So I have a folder in Workspace called "PickedGun", and I have 10 folders in that named "PlayerName". My game allows 10 players in each server, so I want to assign each player to a folder, and have that folder named the player's name. I also wanted to limit the number of items allowed in that folder to 1. The problem is, I have no clue on how I should go about doing this. I've never used the playername thing and I can't figure out how I would go about accomplishing what I want.

--You don't have to make a script for me, just tell me how I should do this.

2 comments

0

In which way are you planning to add items to the folder?

0

cloning a viewmodel in replicated storage and putting it in the folders

2 answers

✓ Accepted answer

So this is a combo: 2 in 1. I can only do the 1st one as I have no idea on how to do the other part. Do this in Workspace as a Script.

game.Players.PlayerAdded:Connect(function(p)
	local folder = Instance.new("Folder",workspace.PickedGun)
	folder.Name = p.Name
end)

game.Players.PlayerRemoving:Connect(function(p)
	workspace.PickedGun:WaitForChild(p.Name):Destroy()
end)

8 comments

0

Make sure to not set the parent of an instance for the second argument!

0

It works, but how would I make this a parent to a model?

0

like for every player?

0

just change the second argument on line 2 and that's your parent

0

No, it's not that simple. In ReplicatedStorage, I have a viewmodel that automatically transfers into the PickedGun folder, and instead of it going there I want it to be parented by the PlayerName folder that your script creates, Anyway, the viewmodel is parented to the workspace by this line of code: Viewmodel.Parent = workspace.PickedGun

0

I tried to modify this to "Viewmodel.Parent = workspace.PickedGun:GetChildren(p.name)" and added the first line of code in your script to the top to define p. It didn't work.

0

Please answer

0

You could probably do the :WaitForChild(p.Name) or :FindFirstChild(p.Name)

First off, you shouldn't make this more complicated than it has to be.

Think of Tables exactly like folders - They store information. You can create a table, and whenever someone joins, add their name to the table. This prevents you from having to create ten folders!

For example:

local allPlayers = {}

game.Players.PlayerAdded:Connect(function(player)
	if not allPlayers[player.Name] then --checks if they are not in the table
		allPlayers[player.Name] = {} -- I assume the items you want added to your original folder were items that related to the player - like values and such? correct me if I'm wrong
	end
end)

Now whenever you want to add information to the player, all you have to do is locate them in the table like so:

local allPlayers = {}

if allPlayers[Player.Name] then
	allPlayers[Player.Name]["Value"] = true
	allPlayers[Player.Name]["OtherValue"] = false
end

--What the table would look like:
local allPlayers = {
	"Player1" = {
		"Value" = true;
		"OtherValue" = false;
	};
}

--To retrieve it (example):
print( allPlayers[Player.Name]["Value"] )

Then, to remove a player when they leave the game, you can simply use PlayerRremoving to check when a player is leaving, and then remove them from the table like so:

local allPlayers = {}

game.Players.PlayerRemoving:Connect(function(player)
	if allPlayers[player.Name] then --checks if they are in the table
		allPlayers[player.Name] = nil --removes them from the table
	end
end)

Now, this is most likely confusing at first, so here are some helpful links!

PlayerAdded, PlayerRemoving, Tables. Make sure to watch some videos for help!

If you need further help, let me know!

3 comments

0

I added 10 folders originally because I wanted to make them parents to viewmodels in replicatedstorage, would that work well with this script?

0

What do you mean by viewmodels?

0

A model that is connected to the camera, and because of the system I use, the viewmodel has to be parented either by workspace or by a descendant of it.

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