:GetAttribute(parameter) attempt to call a nil value

First, I’ve got an error from this script. This script is used to check the Attribute server, and returns it to client.

local RemoteFunction = game:GetService("ReplicatedStorage"):WaitForChild('EventChanged')

function playerAttributeRequired(player, attribute, unit) -- client site input ("FieldOfView", player)
	return unit:GetAttribute(attribute) -- attempt to call a nil value
end

RemoteFunction.OnServerInvoke = playerAttributeRequired

I’m so confused why would that happen. I was guessing maybe "FieldOfView" is not considered to be a string, so I have chenged my code to this:

return unit:GetAttribute(string(attribute))

Then I got another error that called:


attempt to call a table value

I got more confused, why would they get different error when I just turn a String to a String? (maybe)

English is not my main language, sorry for some weird grammar.
I will be appreciated for anyone who answered me. Thank you!

Try using tonumber() instead of string() function. Because FieldOfView Property is numeric data type. Or, if you need string to be returnd, use tostring() instead of string() as @Kacper said.

return unit:GetAttribute(tostring(attribute))

Hope that can help!

Note that tostring() is not a function, but a call to the tostring function.

1 Like

Interesting! May you allow me to ask what is the difference between these functions?

Also, it backwards to attempt to call a nil value again. With this code.

local RemoteFunction = game:GetService("ReplicatedStorage"):WaitForChild('EventChanged')

function playerAttributeRequired(player, attribute, unit)
	return unit:GetAttribute(tostring(attribute))
end

RemoteFunction.OnServerInvoke = playerAttributeRequired

I think that means my parameter is string for sure, but I need to solve this problem:
Why would a string parameter don’t work?

So, if you need a string to be returned anyway, use tostring() instead of string().

return unit:GetAttribute(tostring(attribute))

Edited, I do use tostring(attribute), that’s why I said that.

The problem is still occuring?

Yes, it backs to the title. Attempt to call a nil value.

Can I see your local script? Because this is a server script I guess.

Unit is a nil value. Are you sure when you’re invoking the server you are not passing a player reference?
Your invokeserver should look like this RemoteFunction:InvokeServer(attribute, unit)

1 Like

Right, that’s why I asked for a local script.

I see what happened. I’m so careless by the way.
It was a RemoteFunction for EventRequired. But I have set my RemoteFunction to EventChanged, which is another RemoteFunction.
I have currented that and solved the question. I’m so sorry about that.

local RemoteFunction = game:GetService("ReplicatedStorage"):WaitForChild('EventChanged')
-- Should be:
local RemoteFunction = game:GetService("ReplicatedStorage"):WaitForChild('EventRequired')

Oh okay, good luck scripting :wink:

1 Like