ServerScriptService.Script:37: invalid argument #1 to "pairs" (table expected, got Instance) HELP IM STUCK

So i am making a hitbox system for my game and i am struggling getting this part in the script working a table.

local RS = game:GetService("ReplicatedStorage")
local debris = game:GetService("Debris")
local hitbox = RS.Hitbox

local specialRE = RS.SpecialRE

specialRE.OnServerEvent:Connect(function(plr)
	local char = plr.Character
	local attackingPart = "Right Arm"
	
	local hClone = hitbox:Clone()
	
	local hCFrame = char[attackingPart].CFrame
	
	local weld = Instance.new("WeldConstraint", hClone)
	weld.Part0 = hClone
	weld.Part1 = char[attackingPart]
	
	local chars = {}
	
	for _, part in pairs(GetTouchingParts(hClone)) do
		local echar = part.Parent
		
		if table.find(chars, echar) == nil and echar ~= char then
			
		end
		
		local hum = echar:FindFirstChild("Humanoid")
		
		if hum ~= nil then
			table.insert(chars, echar)
		end
	end
	
	hClone:Destroy()
	
	for _, char in pairs(char) do
		char.Humanoid:TakeDamage(20)
	end
end)

The part where i am struggling is “for _, part in pairs(GetTouchingParts(hClone)) do” please help…

Replace

in pairs(char)

With something that is a table and not a instance, since I don’t know what you are trying to do, you could replace it with this:

for _, char in pairs(char:GetChildren()) do
	char.Humanoid:TakeDamage(20)
end
1 Like

That is not where i have the error. This is where i am struggling:

for _, part in pairs(GetTouchingParts(hClone)) do
		local echar = part.Parent
		
		if table.find(chars, echar) == nil and echar ~= char then
			
		end
		
		local hum = echar:FindFirstChild("Humanoid")
		
		if hum ~= nil then
			table.insert(chars, echar)
		end
	end

At the beginning where it says for _, part in pairs(GetTouchingParts(hClone)) do

By the way what i am trying to do is make a hitbox after a Remote Event is fired

Could you show use the “GetTouchingParts” function. That could really help out.

BasePart | Documentation - Roblox Creator Hub this could help explaining how it works i might be misusing it but i am not sure…

This is confusing due to the fact you are accessing GetTouchingParts in a really weird manner, I’d recommend you to change it, perhaps it could solve it or make the code better in that sense. Your other issue at

	for _, char in pairs(char) do
		char.Humanoid:TakeDamage(20)
	end

has already been told by someone else, but this is wrong behaviour, you want to do pairs on chars (Table defined above the GetTouchingParts loop), not char (defined, but not the expected value)

1 Like

So this script was made by another guy in a tutorial that i was following since i do not know how to create a hitbox system for an ability in my game. Heres the Youtube link of the tutorial (not mine): https://www.youtube.com/watch?v=pwoQKOyGQBM

1 Like

The code on the video is a bit quirky.

image
It is a function that uhh, I’d not recommend using. I’d first gravitate towards doing a system on my own instead of going from a YouTube video, they normally don’t have the best code, its like a canvas you can paint on top of, but not just leave as-is, the reason this is bad is because you are making a connection just for a one time usage. This is not required, the Documentation CLEARLY notes that you only need to enable CanCollide to true, if it was false, the code would also not work anyway, so the thing it is doing, is useless anyway.

I’d rewrite the code like this (And I’ll just made a code dump, becuase I don’t want to make a really creative text being that it comes from a YouTube video and is not yours necessarily, but I will still break down the code a bit).

First of all, no shorting of variable names. Variable names such as RS are complete and utter garbage, they make your code VERY easy to confuse. RS could mean RunService or ReplicatedStorage, this is not really a good thing at all, another source of critisism also comes from using pairs. Why you may ask? Luau has a faster way, which is just NOT using pairs, luau very much deprecated iterator functions (ipairs, pairs, next) when they added the __iter metamethod. This version also attempts to address some possible behaviours that might not be alright, I’d recommend you to test the code before you just use it, and try to understand it, it is commented out with some explanations as to why some things are done, I wrote this quickly, so uhh, don’t trust it much!

local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Not used?
-- local DebrisService = game:GetService("Debris")
local PlayerService = game:GetService("Players")

local hitbox = ReplicatedStorage.Hitbox

local specialRE = ReplicatedStorage.SpecialRE

specialRE.OnServerEvent:Connect(function(player: Player)
	local playerCharacter = player.Character
	local attackingPart = "Right Arm"

	local hClone = hitbox:Clone()

	-- Not used?
	-- local hCFrame = playerCharacter[attackingPart].CFrame

	local weld = Instance.new("WeldConstraint")
	weld.Parent = hClone
	weld.Part0 = hClone
	weld.Part1 = playerCharacter[attackingPart]

	local collidedPlayers = {}

	hClone.CanCollide = true -- Assure that GetTouchingParts works as expected
	task.wait()

	for _, part in (hClone:GetTouchingParts()) do -- pairs is not necessary. Luau has the __iter metamethod, making iterator functions useless.
		local suspectedCharacter: Instance = part.Parent

		local loopPlayer = PlayerService:GetPlayerFromCharacter(suspectedCharacter)

		if not loopPlayer then
			warn("[SpecialRE::OnServerEvent] Touching part is not that of a player?")
			continue
		end

		-- Using PlayerService we can get the player that is going to be the target of the hitbox, making this more reliable.

		if not table.find(collidedPlayers, loopPlayer) then -- Shorten hum ~= nil into hum, it will go through if it is not nil or false.
			table.insert(collidedPlayers, loopPlayer)
		end
	end

	hClone:Destroy()

	for _, loopPlayer: Player in pairs(collidedPlayers) do
		local humanoid = loopPlayer.Character:FindFirstAncestorOfClass("Humanoid")

		if not humanoid then
			warn("Player has no humanoid?")
			continue
		end

		humanoid:TakeDamage(20)
	end
end)
2 Likes

Thanks for the feeedback and i look forward to following ur recommendations, sadly the script seems to have the same issue with table expected, got instance…

Can you provide where the error is?

Line 48 " for _, loopPlayer: Player in pairs(playerCharacter) do " (Almost at the end)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Not used?
-- local DebrisService = game:GetService("Debris")
local PlayerService = game:GetService("Players")

local hitbox = ReplicatedStorage.Hitbox

local specialRE = ReplicatedStorage.SpecialRE

specialRE.OnServerEvent:Connect(function(player: Player)
	local playerCharacter = player.Character
	local attackingPart = "Right Arm"

	local hClone = hitbox:Clone()

	-- Not used?
	-- local hCFrame = playerCharacter[attackingPart].CFrame

	local weld = Instance.new("WeldConstraint")
	weld.Parent = hClone
	weld.Part0 = hClone
	weld.Part1 = playerCharacter[attackingPart]

	local collidedPlayers = {}

	hClone.CanCollide = true -- Assure that GetTouchingParts works as expected
	task.wait()

	for _, part in (hClone:GetTouchingParts()) do -- pairs is not necessary. Luau has the __iter metamethod, making iterator functions useless.
		local suspectedCharacter: Instance = part.Parent

		local loopPlayer = PlayerService:GetPlayerFromCharacter(suspectedCharacter)

		if not loopPlayer then
			warn("[SpecialRE::OnServerEvent] Touching part is not that of a player?")
			continue
		end

		-- Using PlayerService we can get the player that is going to be the target of the hitbox, making this more reliable.

		if not table.find(collidedPlayers, loopPlayer) then -- Shorten hum ~= nil into hum, it will go through if it is not nil or false.
			table.insert(collidedPlayers, loopPlayer)
		end
	end

	hClone:Destroy()

	for _, loopPlayer: Player in pairs(playerCharacter) do
		local humanoid = loopPlayer.Character:FindFirstAncestorOfClass("Humanoid")

		if not humanoid then
			warn("Player has no humanoid?")
			continue
		end

		humanoid:TakeDamage(20)
	end
end)

ahh, I made an issue because I renamed using F2 (Roblox LSP), and didn’t change much more, and I forgot to change the pairs on the playerCharacter and collidedPlayers.

Here is the code with the issue solved.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Not used?
-- local DebrisService = game:GetService("Debris")
local PlayerService = game:GetService("Players")

local hitbox = ReplicatedStorage.Hitbox

local specialRE = ReplicatedStorage.SpecialRE

specialRE.OnServerEvent:Connect(function(player: Player)
	local playerCharacter = player.Character
	local attackingPart = "Right Arm"

	local hClone = hitbox:Clone()

	-- Not used?
	-- local hCFrame = playerCharacter[attackingPart].CFrame

	local weld = Instance.new("WeldConstraint")
	weld.Parent = hClone
	weld.Part0 = hClone
	weld.Part1 = playerCharacter[attackingPart]

	local collidedPlayers = {}

	hClone.CanCollide = true -- Assure that GetTouchingParts works as expected
	task.wait()

	for _, part in (hClone:GetTouchingParts()) do -- pairs is not necessary. Luau has the __iter metamethod, making iterator functions useless.
		local suspectedCharacter: Instance = part.Parent

		local loopPlayer = PlayerService:GetPlayerFromCharacter(suspectedCharacter)

		if not loopPlayer then
			warn("[SpecialRE::OnServerEvent] Touching part is not that of a player?")
			continue
		end

		-- Using PlayerService we can get the player that is going to be the target of the hitbox, making this more reliable.

		if not table.find(collidedPlayers, loopPlayer) then -- Shorten hum ~= nil into hum, it will go through if it is not nil or false.
			table.insert(collidedPlayers, loopPlayer)
		end
	end

	hClone:Destroy()

	for _, loopPlayer: Player in pairs(collidedPlayers) do
		local humanoid = loopPlayer.Character:FindFirstAncestorOfClass("Humanoid")

		if not humanoid then
			warn("Player has no humanoid?")
			continue
		end

		humanoid:TakeDamage(20)
	end
end)
1 Like

So it seems to all work well until we get to the part where it detects if the part being touched is one of a player warning: [SpecialRE::OnServerEvent] Touching part is not that of a player? - Server - HitBoxHandler:31 even tho it is touching a player or rig. Also sometimes it warns about not having a humanoid for no reason… Player has no humanoid? - Server - HitBoxHandler:49

Then check the code logic, it may be interesecting with another part, the Touched objects in the physics of the engine is not restricted to players only

1 Like

Maybe… Also i was checking something with the Hitbox and it seems that when this one is cloned its not placed right in my characters Right Arm

Uhh, you could try Pivoting the hitbox to the right arm. I believe that is what the script was missing

1 Like

So i ended up making a new hitbox system and it actually worked so i will no longer be using this system thanks for the help tho!

1 Like

Alright, good luck with your new system

1 Like

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