Unknown Global 'self'

  1. What do you want to achieve?

I want to remove the unknown global error for self. (self.properties.owner}

  1. What is the issue?

While the script still works I believe it is causing issues elsewhere.

  1. What solutions have you tried so far?

I’ve searched through the dev forums and I’ve also checked elsewhere. While I’ve learned new stuff, I was not able to fix my current issue.

My main issue is I’m noob and I’m sure this is something really silly I’m missing. Any type of guidance would be appreciated.

function script.canSpawnAnimal.OnInvoke(player, animalType)
	local totalCount = getPlayerAnimals(player)
	local playerLevel = game.ServerScriptService.stats.getStat:Invoke(player, "farmLevel")
	local cost, animalLimit, plantLimit = require(game.ReplicatedStorage.libraries.farm).getLevelInformation(playerLevel)
	
	if totalCount >= animalLimit then
		game.ReplicatedStorage.remotes.player.flashUpgrade:FireClient(self.properties.owner)
		return false, "Your animal limit is: "..animalLimit.."!"
	else
		return true
	end

Thanks!

4 Likes

You have to have a class (metatable) to use self in any way, but because you are trying to access it in a script, create the metatable with the variable, and access the data using the variable.

5 Likes

self isn’t defined in this context. self is only automatically defined if the function itself is defined with a colon (i.e. function script.canSpawnAnimal:OnInvoke).

However, judging by your script, you seem to be using a BindableFunction, in which case, this wouldn’t work

4 Likes

I’ve also tried to use player instead of self, while this removes the current error it causes other errors in game.

1 Like

Try defining self to the .OnInvoke Function

function script.canSpawnAnimal.OnInvoke(player, animalType, self)
	local totalCount = getPlayerAnimals(player)
	local playerLevel = game.ServerScriptService.stats.getStat:Invoke(player, "farmLevel")
	local cost, animalLimit, plantLimit = require(game.ReplicatedStorage.libraries.farm).getLevelInformation(playerLevel)
	
	if totalCount >= animalLimit then
		game.ReplicatedStorage.remotes.player.flashUpgrade:FireClient(self.properties.owner)
		return false, "Your animal limit is: "..animalLimit.."!"
	else
		return true
	end

It didn’t work, thanks for the suggestion.

function script.canSpawnAnimal.OnInvoke(player, animalType)
	local totalCount = getPlayerAnimals(player)
	local playerLevel = game.ServerScriptService.stats.getStat:Invoke(player, "farmLevel")
	local cost, animalLimit, plantLimit = require(game.ReplicatedStorage.libraries.farm).getLevelInformation(playerLevel)
	
	if totalCount >= animalLimit then
		game.ReplicatedStorage.remotes.player.flashUpgrade:FireClient(player) -- Here, use 'player' instead of 'self.properties.owner'
		return false, "Your animal limit is: "..animalLimit.."!"
	else
		return true
	end
end

1 Like

Wow thanks a lot, I swear I’ve tried that though!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.