Roblox Studio deciding to ignore some variables of a metatable

Hello. I was trying to port over my server script which handles the punching to a module script; so that I can make the system easier to add on NPCs and such. However I encountered an error which I’m not able to debug at all, which is why I’m posting this here.

This is a snippet of the code that’s causing the issue, as the rest isn’t important to the issue:

Combat.__index = Combat


function Combat.SetCombat()
	return setmetatable({
		Damage = 5, 
		StunDuration = .5,
		Character = nil,
		FX = nil,
		HitboxSize = Vector3.new(4,4,4),
		StartTime = .16,
		StopTime = .25, 
		Offset = CFrame.new(0,0,-2.5),
		MaxCombo = 4
	}, Combat)
end

function Combat:Attack(player)
	
	local character = self.Character
	local humanoid = character:FindFirstChild("Humanoid")

It is then executed in a serverscript:

local punch = game.ReplicatedStorage:WaitForChild("PunchingEvent")
punch.OnServerEvent:Connect(function(player)
	local ServerStorage = game:GetService("ServerStorage")
	local BasicCombat = require(ServerStorage.Modules.BasicCombat)
	local combat = BasicCombat.SetCombat()
	
	
	combat.Character = player.Character
	
	print(player.Character)
	combat.FX = vfx
	
	BasicCombat:Attack()
	
end)

The issue here is that Roblox Studio refuses to recognize the variables from the table, and recognizes them randomly.
The variables it recognizes are those:

  • StopTime (not the StartTime)
  • MaxCombo
  • Offset
  • HitboxSize

It also seems like if I delete one of those variables from the table, Roblox still recognizes them as if they exist.

The error given is when the Humanoid is attempted to be found, as even though the ServerScript defines the player character, the module script for some reason decides to ignore it.

Here’s the error line:

 ServerStorage.Modules.BasicCombat:59: attempt to index nil with 'FindFirstChild'  -  Server - BasicCombat:59

Any help is appreciated

Maybe put a print statement within the Attack function that prints self, or self.Character? Upon inspection, nothing seems to be wrong with your code.

I’ve attempted to do that. The result I get from the modulescript is Nil, while the result I get from the serverscript is the name of the player. It’s a really weird issue.

Oh I see the issue. You need to call Attack on combat, not BasicCombat. Your line at the end should be combat:Attack().

On another note, I see that the function has a parameter player that is not supplied, but that’s unrelated to your issue.

1 Like

This actually works, thanks! As to the player parameter, it’s used to identify the player in the combat.Character = player.Character line. Thanks again

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