Player argument not being passed propperly

This module script function fires a bindable event with the player parameter.

function TheValleyFunctions.GetInventoryFromServer(Player)
	print(Player)
	game.ReplicatedStorage.Inventory.MapInventoryBindable:Fire(Player)
	return game.ReplicatedStorage.Inventory.MapInventoryBindable.Event:Wait()
end

And this is the function that gets called when that event is fired:

local function GetInventoryFromServer(Player)
	warn(Player)
	local PlayerInventory = InventoryTables[Player.UserId]
	print(PlayerInventory, "This is the inventory when mapping")
	RS.Inventory.MapInventoryBindable:Fire(PlayerInventory)
end

RS.Inventory.MapInventoryBindable.Event:Connect(function(Player) GetInventoryFromServer(Player) end)

print(Player) gives me the player name, however warn(Player) in the other script gives me nil. What happened to the Player parameter?

3 Likes

Are both of those scripts on the server or the client?

2 Likes

Both are server scripts, but the script calling the module function gets the player info it needs from another local script.

local function Activated(Player,SelectedIngredient)
	local PlayerGui = Player.PlayerGui.ScreenGUI
	local MolcajeteGui = PlayerGui.Molcajete
	local GrindButton = MolcajeteGui.Grind
	local Inventory = TheValleyFunctions.GetInventoryFromServer(Player)
	warn(Player)
...

Not sure why this information is lost you could try something like this:

function TheValleyFunctions.GetInventoryFromServer(Player)
	print(Player)
	game.ReplicatedStorage.Inventory.MapInventoryBindable:Fire(Player.Name)
	return game.ReplicatedStorage.Inventory.MapInventoryBindable.Event:Wait()
end
local Players = game:GetService("Players")
local function GetInventoryFromServer(PlayerName)
	local Player = Players:FindFirstChild(PlayerName)
	warn(Player)
	local PlayerInventory = InventoryTables[Player.UserId]
	print(PlayerInventory, "This is the inventory when mapping")
	RS.Inventory.MapInventoryBindable:Fire(PlayerInventory)
end

RS.Inventory.MapInventoryBindable.Event:Connect(GetInventoryFromServer(Player))

Sorry for the late response, but I don’t know if this’ll work since the Player parameter in the function is nil, so it would try and grab a nil player by doing doing that.

Why would it grab a nil player?

Does the local script return the player’s name or the player instance?

1 Like

The local script returns the player’s name. I checked earlier with print statements and Player.Parent prints Players. When local function GetInventoryFromServer(PlayerName) is called, PlayerName was nil, so the code would essentially do Players:FindFirstChild(nil)

I would try not using bindable events, I can’t even get one to work the server script never receives a signal

Maybe try a remote event instead

Should I try and get the inventory of the player from the LocalScript instead and pass that as a parameter to Activated() via the remote event? (Activated is triggered by this:

game.ReplicatedStorage.Tools.MolcajeteEvents.GrindItem:FireServer(Player,game.ReplicatedStorage.SelectedIngredient.Value)
game.ReplicatedStorage.Tools.MolcajeteEvents.GrindItem.OnServerEvent:Connect(Activated)

)

1 Like

Something like this might work better

function TheValleyFunctions.GetInventoryFromServer(Player)
	print(Player)
	return game.ReplicatedStorage.Inventory.MapInventoryRemote:InvokeServer(Player) --Use remote function
end
local function GetInventoryFromServer(Player)
	warn(Player)
	local PlayerInventory = InventoryTables[Player.UserId]
	print(PlayerInventory, "This is the inventory when mapping")
	return PlayerInventory 
end

RS.Inventory.MapInventoryRemote.OnServerInvoke = function(Player)
	return GetInventoryFromServer(Player)
end)
1 Like

I would try using a remote function to return the player’s inventory to whatever script calls the remote function

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