Argument 1 missing or nil

Im having trouble getting this raycast hitbox system set up for my entity for the game I am currently making. I keep getting an error on Line 27 saying “Argument 1 missing or nil”, i dont understand whats going on here, so i need some help.

task.wait()
local humanoid = script.Parent:WaitForChild("Humanoid")
local char = script.Parent
local RS = game:GetService("RunService")

RS.Heartbeat:Connect(function()
	if workspace.CurrentEntities:FindFirstChildWhichIsA("Model") then
		local entity
		local allexceptplay = {}
		for i, v in pairs(game.Workspace:GetDescendants()) do
			if not v:FindFirstChild("Humanoid") then
				table.insert(allexceptplay,v)
			end
		end
		for i, v in pairs(workspace.CurrentEntities:GetChildren()) do
			if v.Root then
				entity = v.Root
			end
		end
		local params = RaycastParams.new()
		params.FilterDescendantsInstances = {allexceptplay}
		params.FilterType = Enum.RaycastFilterType.Blacklist
		local origin = entity.Position
		local direction = (char.HumanoidRootPart.Position - entity.Position).Unit * 500
		local ray = workspace:Raycast(origin,direction,params)
		
		local Find = workspace:FindPartOnRay(ray, entity)
		local mognitade = (entity.Position - char.HumanoidRootPart.Position).Magnitude
		if Find then
			if Find:IsDescendantOf(char) then
				if mognitade < 45 then
					humanoid:TakeDamage(humanoid.MaxHealth)
				end
			end
		end
	end

end)
1 Like

entity must be nil.

Looks like you’re looping through something (folder I presume) and if it has a Root inside it, set entity to that. This code will repeatedly reassign entity, though, so I suggested you add a break after assigning.

Your code won’t assign entity if there aren’t any Roots, right before you create params add this line:

print(entity)

Tell me what it prints.

3 Likes

it prints “Root”, which is the main part for the entity, and it looks like its printing the right thing. Also the folder im looping through is the folder where all entities go when they spawn.

The whole entity is a model, but the root is the main part of the model.

1 Like

So it must be the ray, it’s returning nil. Based on the parameters, you’re excluding allexceptplay, by the way you don’t have to put it in a table, just pass it directly:

params.FilterDescendantsInstances = allexceptplay -- this is better

It’s already a table, and you should also use Enum.RaycastFilterType.Exclude, because Blacklist is deprecated.

ray is returning nil because the raycast isn’t hitting anything. If you’re sure it’s going to hit something, it’s a problem, but if you’re not sure it’s going to hit something, you need to check if it did:

if not ray then return end

local Find = workspace:FindPartOnRay(ray, entity)
local mognitade = (entity.Position - char.HumanoidRootPart.Position).Magnitude
if Find then
	if Find:IsDescendantOf(char) then
		if mognitade < 45 then
			humanoid:TakeDamage(humanoid.MaxHealth)
		end
	end
end
1 Like

The purpose of the ray i made is supposed to kill the character, but ignoring all parts that are blocking the character, making it so that there are some safe spots instead of choosing to hide in a locker.

When I used this code you sent, it wouldn’t give me the error anymore, but just wouldn’t kill the player.

This is expected, because your ray isn’t actually ever hitting anything. The code I changed just fixed the error, but I’m not really sure how your system works, and why you need a ray, can you explain what you’re doing?

1 Like

The ray is there because i dont know if theres another way to do the whole safe spot thing, the first thing came to my mind was raycasts.

If there is another solution, may you tell me?

Also this game is like doors, if that explains your question

1 Like

Ohhh, I see now. You’re repeatedly casting to the entities.

Don’t use :FindPartOnRay:

local Find = workspace:FindPartOnRay(ray, entity)
local mognitade = (entity.Position - char.HumanoidRootPart.Position).Magnitude
if Find then
	if Find:IsDescendantOf(char) then
		if mognitade < 45 then
			humanoid:TakeDamage(humanoid.MaxHealth)
		end
	end
end

||
V

local mognitade = (entity.Position - char.HumanoidRootPart.Position).Magnitude
if ray.Instance:IsDescendantOf(char) and mognitade < 45 then
	humanoid.Health = 0
end

Switch the former to the latter, tell me what changes.

Workspace.MisterA2011.Raycast:33: attempt to index nil with ‘Instance’

Don’t forget the other line I had you add, you removed it:

if not ray then return end

The error is gone but yet again it wont kill the character

(even if its not in a blocked area)

Can you print ray? I’m not sure how your game looks, is it printing nil or just hitting something else that’s invisible or obstructing?

it printed nil, thats not good at all.

Can you send the workspace, how does it look?

is it okay if i add you to team create, this game has alot of scattered around stuff
image
here it is anyways

1 Like

Sure, I sent you a friend request.

i forgot that im using an alt on this, can you friend request this instead: EDIFICIOVG807

1 Like

Alright i added you to team create

1 Like

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