What is this error

local playerlist = game.Players:GetChildren()
local dmg = {10}

local metatable = {}

metatable.__add = function(t, t2)
	for i, v in pairs(t) do
		local character = v.Character or v.CharacterAdded:Wait()
		local humanoid = character:FindFirstChild("Humanoid")
		humanoid.Health += -t2[1]
	end
	
	return "Damaged"
end

local function UpdatePlayers()
	playerlist = game.Players:GetChildren()
end

game.Players.PlayerRemoving:Connect(function()
	UpdatePlayers()
	setmetatable(playerlist, metatable)
	print(playerlist + dmg)
end)

the script damages the players but this error pops up everytime someone leaves

1 Like
my old response which i think is wrong

Seems like this is being caused by your CharacterAdded:Wait(), the engine isn’t going to wait to clean up a player instance so your code can finish running.

To put it simply, your code is attempting to yield and delay the C code that is trying to clean up the player instance which the engine obv doesn’t like. To fix this just don’t yield in your __add metamethod, or just don’t call it when a player is leaving.

New response: pretty sure you can’t yield in metamethods

1 Like

in short, metamethods are never permitted to yield

1 Like

how would I access the character when using metatables

your CharacterAdded:Wait() is causing the issue, consider referencing the character elsewhere

Check if the character is already loaded for that player, if it isn’t then skip over them. You can’t yield in metamethods

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