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)
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:
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.
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
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?
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.