Module not printing out ALL the values inside of table?

hey.

so im really heated right now, and just frustrated over this issue. its the fact that my module script wont print all the known values inside of a given table.

instead of printing ALL the values that are inside the table, it only prints ONE of those values, which is so weird to me, that it really aggravates me.

heres the code.

local script

local InitiateCombat = function(b)
	local incombat = chr.Contents:WaitForChild('InCombat')
	if incombat.Value ~= true then
		incombat.Value = true
		CombatModule:InitiateCombat(b)
	else
		return false
	end
end

local monstertable = {}

RunService.Heartbeat:Connect(function(delta)
	local RayParams = RaycastParams.new()
	RayParams.FilterType = Enum.RaycastFilterType.Exclude
	RayParams.FilterDescendantsInstances = {chr}
	RayParams.IgnoreWater = true
	
	local ForwardRaycast = workspace:Raycast(hrp.Position, Vector3.new(0,0,-5), RayParams)
	monstertable = {}
	
	if ForwardRaycast then
		if ForwardRaycast.Instance:FindFirstChild('IsEnemy') then
			local isenemy = ForwardRaycast.Instance:FindFirstChild('IsEnemy')
			local includedMonsters = ForwardRaycast.Instance:FindFirstChild('IncludedMonsters')
			if isenemy.Value == true then
				for i, v in ipairs(includedMonsters:GetChildren()) do
					if v:IsA('StringValue') then
						table.insert(monstertable, v)
						InitiateCombat(monstertable)
						return
					end
				end
				return
			end
		end
	end
	
end)

part of the module script that involves printing out the whole table

local module = {}

function module.Combat (monster, self)
	self = module

	local UIObjects = game:GetService('ReplicatedStorage').UIObjects
	local Monsters = game:GetService('ReplicatedStorage').Monsters
	local combathud = self.plrgui.CombatHud

	print(monster)

	return self
end

return module

please help me, thank you.

3 Likes

i couldn’t figure out where the variable monster is but try printing the type of it, it could be not a table
print(typeof(monster))

1 Like

it is a table.

fwwfewfwfweefwfwfw

maybe it had a __tostring metamethod?

also try printing its members in a loop

could you elaborate on what a tostring metamethod is i’ve never heard of it

it is a way to modify the functionality of tables
__tostring is a metamethod that runs when the tostring function is called on a table, also print() calls tostring on the arguments too
if you donot have __tostring in your code then you donot have to worry about this

example of __tostring
local monsters = {
	Monster1 = "monster1", -- this will print
	Monster2 = "monster2" -- but this willnot print
}

local metaTable = {
	__tostring = function(self)
		return self.Monster1
	end,
}


setmetatable(monsters, metaTable)

local a = tostring(monsters) -- a is "monster1", __tostring metamethod was called

print(monsters) -- print calls __tostring too
--[[
prints
monster1
]]

i cannot figure out what is causing this problem but someone else will probably be able to help

1 Like

The part where you call module.Combat cannot be inferred from the snippet you sent

It’s because of the return statement, I think. You are looping through the includedMonsters children instances, but when you find one, there’s a return statement.

Once you hit that return statement, nothing else in the heartbeat function runs, meaning only one thing ever gets added to the monstertable, and will only print it out once.

You should delete the return that is underneath the InitiateCombat function call, and then move the InitiateCombat call underneath the for loop I think.

Like so:

local InitiateCombat = function(b)
	local incombat = chr.Contents:WaitForChild('InCombat')
	if incombat.Value ~= true then
		incombat.Value = true
		CombatModule:InitiateCombat(b)
	else
		return false
	end
end

local monstertable = {}

RunService.Heartbeat:Connect(function(delta)
	local RayParams = RaycastParams.new()
	RayParams.FilterType = Enum.RaycastFilterType.Exclude
	RayParams.FilterDescendantsInstances = {chr}
	RayParams.IgnoreWater = true
	
	local ForwardRaycast = workspace:Raycast(hrp.Position, Vector3.new(0,0,-5), RayParams)
	monstertable = {}
	
	if ForwardRaycast then
		if ForwardRaycast.Instance:FindFirstChild('IsEnemy') then
			local isenemy = ForwardRaycast.Instance:FindFirstChild('IsEnemy')
			local includedMonsters = ForwardRaycast.Instance:FindFirstChild('IncludedMonsters')
			if isenemy.Value == true then
				for i, v in ipairs(includedMonsters:GetChildren()) do
					if v:IsA('StringValue') then
						table.insert(monstertable, v)
					end
				end
                InitiateCombat(monstertable)
				return
			end
		end
	end
	
end)
1 Like

Not sure if mentioned already, but ipairs in that for loop will stop as soon as it hits a nil value. Maybe check if there are any nil values?

I think the problem is that the parameters self and monster are swapped. For methods (functions using a colon) self will always be the first parameter. Hopefully this helps!

After 2 hours i finally found my solution, with your help! this worked very well for me actually. now i understand what was going on, this really helped. thanks to everyone else who helped too.

1 Like

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