How can i make a random friend generator?

Hello!

I know this is my 4th post on this topic, However i am struggling on how to make a local client sided friend generator

I Feel as if i didn’t make it to clear on what i meant in my other posts. so heres a better explanation:

Similar to this image, I am trying to achieve a system that spawns a limited amount of a players friends and position them at a random part in a folder. I would like this to be client sided so each NPC is different for every players. I would also like it so it only spawns 1 NPC per part.

Can anyone help me out?

You could get the player’s friends list then pick a random lets say 4 out of the table of the friends then just dress the npc’s appearance to the UserId of the friend chosen? I suggest looking up the topic of how to get the players friendslist and maybe for a for loop of the amount of friends they have then do a chance % then do a count to see if its equal to 4 then break out of the loop / end it.

So maybe get the player’s friends list from a server script then do what i said then fire the table of the chosen friends to the client and dress each npc accordingly? If thats what you are asking

1 Like

i dont now how to do that tho, i am not a programmer lol. I should probably learn

Here is something to work off of, it goes through all of the parts in the Workspace Folder Friend Spawn Positions which contains parts where you should spawn a friend and it places a friend one by one through your friends list on those parts.

--[[ Local script ]]--
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local localPlayer = Players.LocalPlayer

local FriendSpawnPositions = game.Workspace:WaitForChild("Friend Spawn Positions")
repeat wait() until #FriendSpawnPositions:GetChildren() == 5 
local spawnPositions = FriendSpawnPositions:GetChildren()

local Rig = ReplicatedStorage.Rig

-- Taken from: https://create.roblox.com/docs/reference/engine/classes/FriendPages
local function iterPageItems(pages)
	return coroutine.wrap(function()
		local index = 0
		while true do
			for _, item in ipairs(pages:GetCurrentPage()) do
				index += 1
				coroutine.yield(index, item)
			end
			if pages.IsFinished then
				break
			end
			pages:AdvanceToNextPageAsync()
		end
	end)
end

local friendPages = Players:GetFriendsAsync(localPlayer.UserId)
for index, item in iterPageItems(friendPages) do
	if index > #spawnPositions then break end
	local UserId = item.Id
	local Appearance = Players:GetHumanoidDescriptionFromUserId(UserId)
	
	local friend = Rig:Clone()
	friend.Parent = game.Workspace
	friend:WaitForChild("Humanoid"):ApplyDescription(Appearance)
	
	friend:MoveTo(spawnPositions[index].Position)
	
end
1 Like

WHAT A LEGEND! THANKS SO MUCH!

Few questions:

  1. Will this show the local players friends or random friends from each player in the server
  2. how can i make these rigs not collideable?
  1. The local players friends

  2. Use collision groups

1 Like

Thanks! it may just be an error but the rigs and parts are falling through the baseplate suddenly. ill just copy and paste the code again. thanks!

Anchor the rig model in replicated or set the collision group for them to work with the parts I believe I forgot what it is called

2 Likes

How can i turn of collisions using this script?
They float in the air as well

local Players = game:GetService("Players")
local Physics = game:GetService("PhysicsService")

Physics:RegisterCollisionGroup("players")
Physics:CollisionGroupSetCollidable("players", "players", false)

local function noCollide(model)
	for _, part in ipairs(model:GetChildren()) do
		if part:IsA("Part") or part:IsA("MeshPart") then
			part.CollisionGroup = "players"
		end
	end
end

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		char:WaitForChild("Humanoid")
		char:WaitForChild("Head")
		char:WaitForChild("HumanoidRootPart")
		wait(0.1)
		noCollide(char)
	end)
        noCollide(char)
end)

Make sure that each of the rigs have Anchored set to False and that the parts that determine their spawn location have CanCollide equal to false.

Then in the Collision group code you gave, make sure that you are adding the rigs to the collision groups and not just the players.

1 Like

Sorted, Thanks!
One final question. How can i set the Rig’s Name to be the friend’s name?

Insert this code snippet to the for loop

friend.Name = item.Username -- Sets the friends name to their username
friend.Humanoid.DisplayName = item.DisplayName -- Overrides usernae to the friends name to their display name

image

Final code with a few changes

--[[ Local script ]]--
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local localPlayer = Players.LocalPlayer

local FriendSpawnPositions = game.Workspace:WaitForChild("Friend Spawn Positions")
repeat wait() until #FriendSpawnPositions:GetChildren() == 5 
local spawnPositions = FriendSpawnPositions:GetChildren()

local Rig = ReplicatedStorage.Rig

-- Taken from: https://create.roblox.com/docs/reference/engine/classes/FriendPages
local function iterPageItems(pages)
	return coroutine.wrap(function()
		local index = 0
		while true do
			for _, item in ipairs(pages:GetCurrentPage()) do
				index += 1
				coroutine.yield(index, item)
			end
			if pages.IsFinished then
				break
			end
			pages:AdvanceToNextPageAsync()
		end
	end)
end

local friendPages = Players:GetFriendsAsync(localPlayer.UserId)
for index, item in iterPageItems(friendPages) do
	if index > #spawnPositions then break end
	local UserId = item.Id
	local Appearance = Players:GetHumanoidDescriptionFromUserId(UserId)

	local friend = Rig:Clone()
	local humanoid = friend:WaitForChild("Humanoid")	
	friend.Parent = game.Workspace
	friend.Name = item.Username -- Sets the friends name to their username
	humanoid.DisplayName = item.DisplayName -- Overrides username to the friends name to their 
	humanoid:ApplyDescription(Appearance) -- Makes the rig look like the friend

	friend:PivotTo(spawnPositions[index]:GetPivot()) -- changed for more consistancy
end
1 Like

Tysm! This will help me with my game so much :smiley:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.