So I have been doing a lot of Bug Testing in ROBLOX Studio and everything seems to work fine. However, whenever I instantiate a part into workspace (manually, not using "Instance.new()"), workspace:Raycast().Instance returns nil! Normally, if the ray doesn't intersect any parts, workspace:Raycast() will return nil, but in this case, workspace:Raycast().Instance returns nil, which is unheard of and never happens unless I instantiate (manually) a part into workspace! Here is my code:
Local script
local RAY_PARAMS = RaycastParams.new()
RAY_PARAMS.FilterDescendantsInstances = IgnoredParts
RAY_PARAMS.FilterType = Enum.RaycastFilterType.Blacklist
local RAY_RESULT = workspace:Raycast(Muzzle.Position, (mouse.Hit.Position - Muzzle.Position).Unit * WeaponData.Range, RAY_PARAMS)
if RAY_RESULT == nil then -- If the ray doesn't intersect any parts.
wait(WeaponData.FireRate)
shooting = false
return
end
local part, pos = RAY_RESULT.Instance, RAY_RESULT.Position
local damage = 0
if part then
if part.Parent:FindFirstChild('Humanoid') then
if part.Name == 'Head' then
damage = WeaponData.HeadDamage
else
damage = WeaponData.Damage
end
end
Events.Shoot:FireServer(part, pos, damage)
end
Server script
Events.Shoot.OnServerEvent:Connect(function(player, part, pos, damage)
local humanoid = part.Parent:FindFirstChild('Humanoid') -- Where the errors are coming from.
print('Ray hit: ' .. part.Name)
if humanoid then
humanoid:TakeDamage(damage)
else
local bullet_hole = Instance.new('Part')
bullet_hole.Anchored = true
bullet_hole.CanCollide = false
bullet_hole.BrickColor = BrickColor.new('Really black')
bullet_hole.Material = Enum.Material.SmoothPlastic
bullet_hole.Size = Vector3.new(0.1, 0.1, 0.1)
bullet_hole.Position = pos
bullet_hole.Parent = workspace.RayIgnoredParts
game:GetService('Debris'):AddItem(bullet_hole, 5)
end
end)
Of course, I'm not checking to see if the part is nil, but it shouldn't. The "Instance" property in workspace:Raycast().Instance happens to be nil even though the ray hits a part. Can someone help me with this problem?
EDIT: This might be some internal ROBLOX bug. I rewrote my Server script and this is what it looks like:
Events.Shoot.OnServerEvent:Connect(function(player, part, pos, damage)
local humanoid = nil
if part then
humanoid = part.Parent:FindFirstChild('Humanoid')
print('Ray hit: ' .. part.Name)
else
print('Ray hit: INSTANCE_NIL')
end
if humanoid then
humanoid:TakeDamage(damage)
else
local bullet_hole = Instance.new('Part')
bullet_hole.Anchored = true
bullet_hole.CanCollide = false
bullet_hole.BrickColor = BrickColor.new('Really black')
bullet_hole.Material = Enum.Material.SmoothPlastic
bullet_hole.Size = Vector3.new(0.1, 0.1, 0.1)
bullet_hole.Position = pos
bullet_hole.Parent = workspace.RayIgnoredParts
game:GetService('Debris'):AddItem(bullet_hole, 5)
end
end)
I found out that workspace:Raycast().Instance returns nil when instantiating (manually) parts to the workspace, but workspace:Raycast().Position doesn't. If the problem is just me, please provide me with a clear answer.
