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.
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
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
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)
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
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