i want to make a npc that is player’s random friend
I tried others script but it say unable to cast string to int64
Use the :GetFriendsAsync()
method found in the Players
service:
local success, friensPages = pcall(game.Players:GetFriendsAsync(your_UserId))
-- always wrap asynchronous calls in a pcall since they tend to fail
-- (which will terminate your thread execution (aka stop code))
This method will return a FriendsPages
object, which contains a player’s friends, their UserIds, Usernames and Online status. They’re not tables, but rather Instances, so you can convert it into a table:
-- Code stolen from docs lol
local function iterPageItems(pages)
return coroutine.wrap(function()
local pagenum = 1
while true do
for _, item in ipairs(pages:GetCurrentPage()) do
coroutine.yield(item, pagenum)
end
if pages.IsFinished then
break
end
pages:AdvanceToNextPageAsync()
pagenum += 1
end
end)
end
local success, friendPages = pcall(game.Players:GetFriendsAsync(your_UserId))
local userIds = {}
for item, _pageNo in iterPageItems(friendPages) do
table.insert(userIds, item.Id)
end
You can then get a HumanoidDescription for a random UserId:
local randomFriend = userIds[math.random(1, #userIds)]
local HumanoidDescription = Players:GetHumanoidDescriptionFromUserId(randomFriend)
And finally apply this HumanoidDescription to a rig (NPC):
local Rig = workspace.Rig -- get your rig
Rig.Humanoid:ApplyDescription(HumanoidDescription)
Or you can directly create a model from the UserId:
Players:CreateHumanoidModelFromUserId(randomFriend)
for item, _pageNo in iterPageItems(friendPages) do
it say attempt to call missing method ‘GetCurrentPage’ of string
My bad, called pcall the wrong way. Should be fixed here:
local Players = game:GetService("Players")
local success, friendPages = pcall(function()
return Players:GetFriendsAsync(UserIdHere)
end)
if not success then
-- remember to handle pcall errors
end
local function iterPageItems(pages)
return coroutine.wrap(function()
local pagenum = 1
while true do
for _, item in ipairs(pages:GetCurrentPage()) do
coroutine.yield(item, pagenum)
end
if pages.IsFinished then
break
end
pages:AdvanceToNextPageAsync()
pagenum += 1
end
end)
end
local userIds = {}
for item, _pageNo in iterPageItems(friendPages) do
table.insert(userIds, item.Id)
end
local randomFriend = userIds[math.random(1, #userIds)]
local HumanoidDescription = Players:GetHumanoidDescriptionFromUserId(randomFriend)
local Rig = workspace.Rig -- replace with your rig
Rig.Humanoid:ApplyDescription(HumanoidDescription)
it works but is there a way to do humanoid:ApplyDescription with local script?
Of course. Put that code in a LocalScript.
and it say humanoid:ApplyDescription can only be called by the backend server
I was typing a code when I saw your question, but I had to go afk, @Vanniris code is fine, works as intended. But it wont work client sided if you dont create the Rig from a client script, otherwise it will say you cannot change the Description
Yup, thats because the rig exist in server side since the beginning, and a client cant edit a serversided description, the rig should be created from client side to edit that
Just instead of this:
local HumanoidDescription = Players:GetHumanoidDescriptionFromUserId(randomFriend)
local Rig = workspace:WaitForChild("Rig") -- replace with your rig
Rig.Humanoid:ApplyDescription(HumanoidDescription)
Use this:
local newFriend = Players:CreateHumanoidModelFromUserId(randomFriend)
newFriend.Parent = game.Workspace
@Vanniris already suggested to use CreateHumanoidModelFromUserId()
instead of editing an existing Rig in workspace
OMG It works TYSM for all guys that helped me!
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.