Attempt to index nil with 'Value'

Attempting to get the closest value from a array for a small project.

I have tried two different methods, closest.Value and just .closest.

I get errors either way.

	local HowMany = math.random(100,300)
    local PlayerGuesses = {} --numbers get added here in-game

	for i, currentNum in pairs(PlayerGuesses) do

		local diff = math.abs(currentNum - HowMany)

		if diff < closest then
			closest = currentNum
		end

	end
	
	
	Status.Value = "Winner : "..closest.Value

Where is closest? Try adding local closest = 0 at the top of your script. Then, replace the closest.Value with just tostring(closest).

local HowMany = math.random(100,300)
local PlayerGuesses = {}
local closest = nil
local lowestDiff = math.huge

for i, currentNum in pairs(PlayerGuesses) do

	local diff = math.abs(currentNum - HowMany)

	if diff < lowestDiff then
		lowestDiff = diff
		closest = currentNum
	end

end

Status.Value = "Winner : "..closest

This did make it work, but it keeps it set to zero, it doesnt change to the zero.


As you can see I guesses 275 and the correct number was 265

I see, so closest is an object value. Replace local closest = 0 with local closest, and "Winner : "..tostring(closest) with "Winner : "..(closest and closest.Name) or "nobody won"

The winner box just replies with “nil” now.
(I didnt add the “nobody won” part because there must be a winner no matter what)
image

What does the table of PlayerGuesses contain? Does it contain Player objects?

Have you tried what I replied with? You have to compare the number to something to start with which would be math.huge.

1 Like

This is what added the numbers to the array.

local PlayerGuesses = game.ReplicatedStorage.Events.Number:InvokeServer()

script.Parent.TextBox.FocusLost:Connect(function()
	if script.Parent.TextBox.Text == not "" then
		--x
	else
		local num = script.Parent.TextBox.Text
		table.insert(PlayerGuesses, tostring(num))
		print(PlayerGuesses)
	end
end)

Still replies with nil when trying that method

It works for me. Run this in the command bar. I also changed the values in the PlayerGuesses table to strings and it still works.

local HowMany = math.random(100,300)
local PlayerGuesses = {"100", "150", "200", "250"}
local closest = nil
local lowestDiff = math.huge

for i, currentNum in pairs(PlayerGuesses) do

	local diff = math.abs(currentNum - HowMany)

	if diff < lowestDiff then
		lowestDiff = diff
		closest = currentNum
	end

end

print("HowMany = "..HowMany)
print("Closest = "..closest)
1 Like

I think I see the issue now. PlayerGuesses isn’t an instance, it is a number. You can’t have a number for both of them.

^ How would I fix this?

and this is what I changed it to and it still sets winner to nil.

local PlayerGuesses = {}
local closest
local lowestDiff = math.huge

for i, currentNum in pairs(PlayerGuesses) do

	local diff = math.abs(currentNum - HowMany)

	if diff < closest then
		lowestDiff = diff
		closest = currentNum
	end

end
	
	
Status.Value = "Winner : "..tostring(closest)

Add a value into each player named plrguess and then use that instead.

Okay I added this into ServerScriptService

game.Players.PlayerAdded:Connect(function(player)
	local StatFolder = Instance.new("Folder")
	StatFolder.Name = "StatFolder"
	StatFolder.Parent = player
	
	local IntValue = Instance.new("IntValue")
	IntValue.Name = "plrguess"
	IntValue.Value = nil
	IntValue.Parent = player.StatFolder
end)

This is comparing diff to nil. It should be if diff < lowestDiff then

Still is nil

You need to check if the closest value is nil and then if it is you will need to set it as something i just need to know what your goal is. What is the closest variable needed for?

1 Like

I’m trying to make a game where there is a random amount of items set into a jar and then each players guesses and whoever is closest wins the round.

Video if you are interested in how it looks so far : 2022-10-16 15-33-42

Oh i see so what you need to do is check if the closest value is nil and then set the closest value to the current players guesses

1 Like