Position is not a valid member of Ray

I’m making a gun system and I want to add bullet holes, the gun system that I’m using can be found here:

And I’m using this to help w/ the bullet holes.

The error I keep getting is “Position is not a valid member of Ray”. I dont know if this like a dummy question but I need help fixing it.

Here is the code for the raycast (I am not sure which one to choose)

		local ray = Ray.new(from.Position, from.CFrame.lookVector*500);
		local hit, pos, normal = game.Workspace:FindPartOnRayWithIgnoreList(ray, {self.humanoid.Parent, self.weapon});

And for the bullet hole:

        local Weld = Instance.new("Weld")

	print(pos)
	
	local clone = bullet_hole:Clone()
	
	clone.Parent = game.Workspace.Bullet_Holes
	
	
	clone.CFrame = CFrame.new(pos.Position, pos.Position + pos.Normal)
	
	local WeldConstraint = Instance.new("WeldConstraint", clone)
	WeldConstraint.Part0 = clone
	Weld.Constraint.Part1 = pos.Instance
	
	wait(3)
	
	clone:Destroy()

Feel free to ask for any more code or any pictures or videos.

1 Like

Ray is deprecated use workspace:Raycast()


if pos is a Vector3 then you are getting the error because you are trying to get pos.Position

clone.CFrame = CFrame.new(pos.Position, pos.Position + pos.Normal)

I would think the error would be “Position is not a valid member of Vector3” and not “Position is not a valid member of Ray” but what do I know.

local ray = workspace:Raycast(from.Position, from.CFrame.lookVector*500);

I changed it to this but I’m getting a new error now.

	local hit, pos, normal = game.Workspace:FindPartOnRayWithIgnoreList(ray, {self.humanoid.Parent, self.weapon});

Thats the line where it breaks…

local rayOrigin = part.Position
local rayDirection = part.CFrame.LookVector * 500
local raycast = workspace:Raycast(rayOrigin, rayDirection)

if raycast then
    local position = raycast.Position
    -- stuff
end

Idk if this would work. plus the raycast is a module script

local r = RaycastParams.new()
r.FilterDescendantsInstances = {self.humanoid.Parent, self.weapon}
Local ray = workspace:Raycast(from.Position,from.CFrame.LookVector*500,r)
if ray then
      print(ray.Position,ray.Normal)
end
--Module
function fps:fire()
	if (self.isEquipped and self.isAlive) and enableGun == true then
		local r = RaycastParams.new()
		r.FilterDescendantsInstances = {self.humanoid.Parent, self.weapon}
		local from = self.isAiming and self.weapon.Aim or self.weapon.Shoot;
		local ray = workspace:Raycast(from.Position, from.CFrame.lookVector*500, r);
		--local hit, pos, normal = workspace:FindPartOnRayWithIgnoreList(ray, {self.humanoid.Parent, self.weapon});
		local hit, pos, normal = workspace:FindPartOnRayWithIgnoreList(ray, {self.humanoid.Parent, self.weapon})
		local shotgun_ray
		
		--for i = 1, pelletCount do --Create a loop that repeats itself based on the amount of pellets being shot
			--local pelletSpread = Vector3.new(rng:NextNumber(-maximumOffset, maximumOffset), rng:NextNumber(-maximumOffset, maximumOffset), rng:NextNumber(-maximumOffset, maximumOffset)) --Create a Vector3 that consists of a randomization of all the axes.
			--ray = Ray.new(from.Position, from.CFrame.lookVector+pelletSpread*500) --Create an equation of Mouse.Hit.LookVector + the pelletSpread.  
		--end
		
		
		local enemyHum;
		if (hit) then
			local players = game.Players:GetPlayers();
			for i = 1, #players do
				if (players[i].Character and hit:IsDescendantOf(players[i].Character)) then
					enemyHum = players[i].Character:FindFirstChild("Humanoid");
					break;
				end
			end
			if (enemyHum) then
				enemyHum:TakeDamage(self.settings.damageFunc())
			end
		end
		
		self.recoil.p = self.settings.recoilFunc();
		remotes.fire:FireServer(self.Right, self.Left, enemyHum);
		if ray.Instance then
			remotes.raypos:FireServer(ray.Position, ray.Normal)
		end

		
		print(pos)
		
		local serverWeapon = self.humanoid.Parent:FindFirstChild(self.weapon.Name)
		local fireSound = serverWeapon.Handle:FindFirstChild("Fire");
		if (fireSound) then
			fireSound:Play();
		end
	end
end

-- Server
remotes.raypos.OnServerEvent:Connect(function(player , pos, normal)
	
	print(pos)
	
	local clone = bullet_hole:Clone()
	
	clone.Parent = game.Workspace.Bullet_Holes
	
	
	clone.CFrame = CFrame.new(pos.Position, pos.Position + normal)
	
	local WeldConstraint = Instance.new("WeldConstraint", clone)
	WeldConstraint.Part0 = clone
	Weld.Constraint.Part1 = pos.Instance
	
	wait(3)
	
	clone:Destroy()
	
	
end)

Same error:
Unable to cast RaycastResult to Ray and ContextActionService: Unexpected error while invoking callback: Unable to cast RaycastResult to Ray

You need to check if the ray hit something (if ray) rather than if the ray.Instance (if ray.Instance) exists - that is why.

ERROR:	Unable to cast RaycastResult to Ray and ContextActionService: Unexpected error while invoking callback: Unable to cast RaycastResult to Ray

If we read the error, it says that the WorldRoot:FindPartRayWithIgnoreList is expecting a Ray, but is instead getting a RaycastResult instead, causing the error.

In fact, you don’t need the method at all, you can instead replace them with the ray since they have the information already.

local hit, pos, normal = ray.Instance, ray.Position, ray.Normal

But like what @CharIomagne said, you need to stop the function if the ray variable didn’t return anything.

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