Return player in function doesn't actually end up returning

I am working on a script which allows you to attach certain medical items to a player you click on. By clicking on them it fetches their character and name, showcased here:
https://gyazo.com/972a56699b92dd1c24f623ed0fc35085

The problem is, since I want the items to be attached to the character in the server so it isn’t local I fire off a RemoteEvent which sends both the name of the Player and the name of the PlayerTarget. These are translated into Player and Patient in the server script. The player which gets returned in the function showcased here:

local function toolActivated(player)
...
	if Character and game.Players:GetPlayerFromCharacter(Character) then
		local PlayerTargeted = game.Players:GetPlayerFromCharacter(Character)
		local player = PlayerTargeted
...
		return player
	...
		
	end
	
end

To send the player function to the server I use the following:

local targetplayer = toolActivated(player)
...
bvmbutton.MouseButton1Click:Connect(function()
	if enabled then
		bvmevent:FireServer(targetplayer)
	else
		warn("Error1")
	end
end)

The player in the “local targatetplayer=toolActivated(player)” is underlined with “unknown global ‘player’”

I have 0 clue how to fix this as I am not that good of a scripter. Any explenation as to how I can fix this is very much appreciated!

1 Like

Also, there aren’t any errors except for the “local targetedplayer=toolActivated(player)”

Could you post your full code? Otherwise I’d return PlayerTargeted instead of returning local player;

Returning PlayerTargeted still yields the same result. “unknown global”

Simply means that player is not being set to anything. And you are basically pulling out variables out of thin air.

Player is being set to the value of the players name? How exactly am I pulling out variables out of thin air.

Variables are created by clicking on the other player which collects all needed data.

The variable you pass to toolActivated, player, is pulled out of thin air apparently. Show me where player is defined.

Above is the part where player is assigned. The character is grabbed via a mouse.target section above.

You’re also defining player inside the toolActivated call as a param.

Which is the variable that is erroring.

image
For example here, no where in my code is player being defined that’s why it’s erroring only in the test function which is an entirely different scope and player is being set locally.

Fixed that issue. PlayerTargeted now does exist and the global error is gone.

Yet it now still only passes the localPlayers name and not the TargetedPlayer even tho I clearly tell it to do so.

Some more bug finding gave me the following results:

In this part:

if Character and game.Players:GetPlayerFromCharacter(Character) then
		local PlayerTargeted = game.Players:GetPlayerFromCharacter(Character)
		local player = PlayerTargeted.Name
		print("Player",player)
		print("Character",Character)

Character actually prints the targeted player YET

When I return that value and try to use it in the ServerEvent it returns NIL

bvmbutton.MouseButton1Click:Connect(function()
	if enabled then
		print(targetplayer.name,Player)
		bvmevent:FireServer(Player,targetplayer)

Are you sure it is actually nil or is it just printing nil because you are indexing name on targetplayer although the function returns the player’s name.

I’m going to assume the toolActivated code snippet you provided does a raycast to get the Character variable (which would be correct). However you are calling toolActivated when the script first runs, not inside the MouseButton1Click callback function which is likely the issue.