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

Player is called a userdata value?

I tried making an NPC that would follow the closest player in the game. Here's what I did.

(This script is parented to the NPC's Humanoid)

while true do wait()
	local closestPlayer
	local distance
	local plrs = game.Players:GetPlayers()
	
	for i = 1, #plrs do
		local currentDistance = plrs[i]:DistanceFromCharacter(script.Parent.Parent.Head.Position)
		if distance == nil then
			closestPlayer = plrs[i]
			distance = currentDistance
		elseif currentDistance > distance then
			closestPlayer = plrs[i]
			distance = currentDistance
		end
	end
	print(closestPlayer..", "..distance)
	wait(.5)
end

The problem is that "closestPlayer" is a "userdata value"...?

I'm not sure what this could mean, but please show me what's going wrong here; I would love to have this problem fixed ASAP.

2 comments

0

Assuming my answer helped and was correct in fixing the problem, you wouldnt mind accepting it would you? X)

0

XD Sorry, I've completely forgot multiple times

2 answers

✓ Accepted answer

Improvements

Use GetService() to retrieve the Players Service

Use in pairs on tables to define the index and value together instead of separately

Use WaitForChild() on any instance which may not have loaded before the script

Issues

Player is a UserData value refers to your attempt to print the player instance in your code instead of the player's Name property (a string value)

The other error you can receive is having distance be nil, which again, you cant print, so you should make sure the player and distance are both valid variables before performing the print

Revised Server Script

local npc = script.Parent.Parent

while true do
	local closestPlayer = nil
	local distance = nil

	for index, player in pairs(game:GetService("Players"):GetPlayers()) do
		local currentDistance = player:DistanceFromCharacter(npc:WaitForChild("Head").Position)
		if distance == nil then
			closestPlayer = player
			distance = currentDistance
		elseif currentDistance > distance then
			closestPlayer = player
			distance = currentDistance
		end
	end

	if closestPlayer and distance then
		print(closestPlayer.Name..", "..distance)
	end
	wait(0.53)
end

2 comments

0

I previously was using the "in pairs()" function for this, but thought it was the cause of the problem. Thank you for using your precious time on answering this question. I really appreciate it.

0

np!

Hi, that's a good question.

Essentially, userdata are C objects Roblox is using. In this case an Instance (the Player instance).

That code is throwing an error because you're trying to concatenate closestPlayer which is an Instance, and Roblox doesn't know what to do when this happens. A good solution would be to use closestPlayer.Name instead, as Roblox can concatenate strings.

1 comment

0

"The compiler", not "Roblox"

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