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!
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.
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.