Attempt to call missing method 'GetPlayerFromCharacter' of table (module)

So I didn’t know what was happening with my module script when I saw this message in the output.

Alexa_BoyzCupcake got hit by Hamburger  -  Server - Hamburger:34
  11:00:05.420  ServerScriptService.Server.Hamburger:35: attempt to call missing method 'GetPlayerFromCharacter' of table  -  Server - Hamburger:35
  11:00:05.420  Stack Begin  -  Studio
  11:00:05.420  Script 'ServerScriptService.Server.Hamburger', Line 35 - function FindPlayers  -  Studio - Hamburger:35
  11:00:05.420  Script 'ServerScriptService.Server.Hamburger', Line 53  -  Studio - Hamburger:53
  11:00:05.420  Stack End  -  Studio

So that’s why I needed help on this. Here’s part of the module script.

if result and result.Instance then
				local hit = result.Instance
				if hit == obj or hit:FindFirstAncestor(obj.Parent.Name) then
					local damage = 100
					local predictedHealth = obj.Parent.Humanoid.Health - damage
					obj.Parent.Humanoid.Health = obj.Parent.Humanoid.Health - damage
					if predictedHealth < 1 then
						print(obj.Parent.Name.. " got hit by ".. instancename)
						jumpscare:FireClient(players:GetPlayerFromCharacter(obj.Parent))
					end
				end
			end
1 Like

Can you show me what players is defined as?

1 Like

To cut things short, though players was meant to be a reference to the Players service, you accidentally assigned it to the wrong value (a table). Double-check how you make this assignment and rectify any mistakes

1 Like
local players = players:GetPlayers()

It could be that you overrode the the value. Use Ctrl + F and input “players =” into the search bar. This will help you locate all instances of an assignment to that variable

1 Like

Oh wait no it wasn’t the get service one. It was:

local players = players:GetPlayers()

And there you go! These mistakes can happen. Make sure to recall this type of situation and use the strategies I mentioned

1 Like

No I don’t mean it’s the solution. I meant a correction. But here’s the video

Oh, I’m sorry, I though you put two and two together. As I said earlier, you mean for players to refer to the Players service. This is achieved with the following code

local Players = game:GetService("Players")
-- Roblox style guide suggests PascalCase for imports.

You assigned it to the result of a Players:GetPlayers call, however, which results in an array (table) of players

1 Like

Well if I changed it to that, then some things wouldn’t work like these ones.

function hamburger.FindPlayers(model)
	local players = players:GetPlayers()
	local characters = {}
	for i, player in ipairs(players) do
		if player.Character then
			table.insert(characters, player.Character)
		end
	end
	
	local overlapParams = OverlapParams.new()
	overlapParams.FilterType = Enum.RaycastFilterType.Include
	overlapParams.FilterDescendantsInstances = characters
	local collisions = workspace:GetPartsInPart(model.Hitbox, overlapParams)
	
	for indev, obj in ipairs(collisions) do
		if obj.Name == "HumanoidRootPart" then
			local rayDirection = obj.Position - model.Hamburger.Position
			local result = workspace:Raycast(model.Hamburger.Position, rayDirection)
			if result and result.Instance then
				local hit = result.Instance
				if hit == obj or hit:FindFirstAncestor(obj.Parent.Name) then
					local damage = 100
					local predictedHealth = obj.Parent.Humanoid.Health - damage
					obj.Parent.Humanoid.Health = obj.Parent.Humanoid.Health - damage
					if predictedHealth < 1 then
						print(obj.Parent.Name.. " got hit by ".. instancename)
						jumpscare:FireClient(players:GetPlayerFromCharacter(obj.Parent))
					end
				end
			end
		end
	end
end

This is a classic case of variable shadowing. This is not a technique but an anti-pattern. You should rarely if ever define a variable with the same name as another:

local players = players:GetPlayers()

Change your original definition of “players” to be “Players” to avoid the overwrite:

local players = Players:GetPlayers()

It seems like you have no need for the array of players beyond its use for the collection of all in-game characters, so you could also rewrite that code:

local characters = {}

for _, player in players:GetPlayers() do
	if player.Character then
		table.insert(characters, player.Character)
	end
end
1 Like

I still got the error.

character limit

I think you made a mistake in implementing one of the solutions

1 Like

Well for a full backup, here’s the full script I made.

mostly from a tutorial

local runservice = game:GetService("RunService")
local Players = game:GetService("Players")
local debounce = false
local jumpscare = game.ReplicatedStorage.JumpscareEvents.Hamburger
local instancename = "Hamburger"

local hamburger = {}

function hamburger.FindPlayers(model)
	local players = Players:GetPlayers()
	local characters = {}
	for i, player in ipairs(players) do
		if player.Character then
			table.insert(characters, player.Character)
		end
	end
	
	local overlapParams = OverlapParams.new()
	overlapParams.FilterType = Enum.RaycastFilterType.Include
	overlapParams.FilterDescendantsInstances = characters
	local collisions = workspace:GetPartsInPart(model.Hitbox, overlapParams)
	
	for indev, obj in ipairs(collisions) do
		if obj.Name == "HumanoidRootPart" then
			local rayDirection = obj.Position - model.Hamburger.Position
			local result = workspace:Raycast(model.Hamburger.Position, rayDirection)
			if result and result.Instance then
				local hit = result.Instance
				if hit == obj or hit:FindFirstAncestor(obj.Parent.Name) then
					local damage = 100
					local predictedHealth = obj.Parent.Humanoid.Health - damage
					obj.Parent.Humanoid.Health = obj.Parent.Humanoid.Health - damage
					if predictedHealth < 1 then
						print(obj.Parent.Name.. " got hit by ".. instancename)
						jumpscare:FireClient(players:GetPlayerFromCharacter(obj.Parent))
					end
				end
			end
		end
	end
end

function hamburger.LerpTo(model, target)
	local alpha = 0
	local speed = 50
	local distance = (model.PrimaryPart.Position - target.Position).Magnitude
	local relativeSpeed = distance / speed
	local startCFrame = model.PrimaryPart.CFrame
	local loop = nil
	local reachedTarget = Instance.new("BindableEvent")
	
	loop = runservice.Heartbeat:Connect(function(delta)
		hamburger.FindPlayers(model)
		local goalCFrame = startCFrame:Lerp(target.CFrame, alpha)
		model:PivotTo(goalCFrame)
		alpha += delta / relativeSpeed
		if alpha >= 1 then
			loop:Disconnect()
			reachedTarget:Fire()
		end
	end)
	
	reachedTarget.Event:Wait()
	
end

function hamburger.Navigate(model, prevNum, maxNum, generatedRooms)
	for i=prevNum, maxNum do
		local room = generatedRooms[i]
		hamburger.LerpTo(model, room.Entrance)
		
		local waypoints = room:FindFirstChild("Waypoints")
		if waypoints then
			for i=1, #waypoints:GetChildren() do
				hamburger.LerpTo(model, waypoints[i])
			end
		end
		
		hamburger.LerpTo(model, room.Exit)
		room.Door.OpenEvent:Fire()
	end
end

function hamburger.New(number, generatedRooms, maxTravel)
	
	local enemyModel = workspace.Enemies.Hamburger:Clone()
	
	local prevNum = number - 2
	local maxNum = number + maxTravel
	local prevRoom = generatedRooms[prevNum]
	if not generatedRooms[maxNum] then
		maxNum = #generatedRooms
	end
	local maxRoom = generatedRooms[maxNum]
	
	enemyModel:PivotTo(prevRoom.Entrance.CFrame)
	enemyModel.Parent = workspace
	enemyModel.Hamburger.Far:Play()
	enemyModel.Hamburger.Near2:Play()
	enemyModel.Hamburger.Near:Play()
	
	hamburger.Navigate(enemyModel, prevNum, maxNum, generatedRooms)
	
	local lastDoor = maxRoom.Door
	lastDoor.OpenEvent:Fire()
	
	enemyModel:Destroy()
end

return hamburger

You’re still using players as the root for your “GetPlayerFromCharacter” call. Change this so it uses the “Players” variable as it holds the reference to the service

1 Like

Oh right I forgot about that. Let me try that for a second.

Yep! It works! Sorry for the late response.

1 Like

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