Raycasting issue (NewInstance Size)

The NewPart doesn’t end up hitting the target (because the position is stuck as the same even if the size is changed) . Also sometimes the size is stuck at default when no instance is found.
How can this issue be solved?

local Tool = script.Parent
local RS = game:GetService("ReplicatedStorage")
local RE = RS.ToolRE
local Part = Tool.Part
local Active = true

RE.OnServerEvent:Connect(function(Player, MousePos)
	local PartPos = Part.Position
	local RayDirection = (MousePos - PartPos).Unit
	local Raycast = workspace:Raycast(MousePos, RayDirection)
	local NewPart = Instance.new("Part")
	--NewPart.Shape = Enum.PartType.Ball
	NewPart.CanCollide = false
	NewPart.CanQuery = false
	NewPart.Anchored = true
	NewPart.CFrame = CFrame.new(PartPos, MousePos)
	NewPart.Position = Part.Position
	NewPart.Velocity = NewPart.CFrame.LookVector * 300
	NewPart.Parent = workspace
	
	if Raycast then
		local Distance = (Part.Position - Raycast.Instance.Position).Magnitude
		print(Distance)
		NewPart.Position = PartPos + Vector3.new(0, 0, 1)
		NewPart.Size = Vector3.new(1, 1, Distance)
		print(NewPart.Size)
		print(Raycast.Instance)
	end
	NewPart.Touched:Connect(function(Hit)
		local Humanoid = Hit.Parent:FindFirstChildOfClass("Humanoid")
		if Hit and Humanoid and Active then
			Active = false
			Humanoid:TakeDamage(11)
			wait(0.100)
			Active = true
		end
	end)
end)
nothing to see here

:point_right: :point_left:

1 Like

I think the fact that its anchored is probably the reason why. You can try weldconstraints instead.

Isn’t the problem the position though?

I’m not sure what you mean by your first part then. The position would remain the same if the part is anchored.

This is one of your main issues!
instead of positioning the NewPart at the old Part, position it here:
NewPart.CFrame = CFrame.new(PartPos+(RayDirection*(Distance/2)), MousePos)
this will position it in the middle of the target pos and the origin pos and will look at the target pos (so it is rotated the correct way)!
Another issues I see is in your Distance formula! You should use:
local Distance = (Part.Position - Raycast.Position).Magnitude
You code is pretty error friendly too! Try adding more if statements to accord for everything!:

if Raycast and Raycast.Position then
 --do code here 
end
1 Like

This is also very wrong. You calculated the direction correctly but the directional vector has the length of 1! That’s why we have to multiply it by a “MaxRayLength” factor!
And the Raycast origin is typically not at the target, but at the origin - so in this case the PartPos!

This is the improved version and you should still read my other posts pointing out your mistakes so that you can learn something from this!

local Tool = script.Parent
local RS = game:GetService("ReplicatedStorage")
local RE = RS.ToolRE
local Part = Tool.Part
local Active = true

RE.OnServerEvent:Connect(function(Player, MousePos)
	local RayDirection = (MousePos - Part.Position).Unit
	local MaxRayLenght = 1000
	local RCPM = RaycastParams.new()
	RCPM.FilterType = Enum.RaycastFilterType.Exclude
	RCPM.FilterDescendantsInstances = {Player.Character}
	
	local Raycast = workspace:Raycast(Part.Position, RayDirection*MaxRayLenght,RCPM )
	local NewPart = Instance.new("Part")
	--NewPart.Shape = Enum.PartType.Ball
	NewPart.CanCollide = false
	NewPart.CanQuery = false
	NewPart.Anchored = true
	NewPart.CFrame = CFrame.new(PartPos, MousePos)
	NewPart.Position = Part.Position
	NewPart.Velocity = NewPart.CFrame.LookVector * 300
	NewPart.Parent = workspace

	if Raycast and Raycast.Position then
		local Distance = (Part.Position - Raycast.Position).Magnitude
		NewPart.Size = Vector3.new(1, 1, Distance)
		NewPart.CFrame = CFrame.new(PartPos+(RayDirection*(Distance/2)), MousePos)
		
		print("Distance: "..Distance)
		print("Dimensions: "..NewPart.Size)
	end
	NewPart.Touched:Connect(function(Hit)
		local Humanoid = Hit.Parent:FindFirstChildOfClass("Humanoid")
		if Hit and Humanoid and Active then
			Active = false
			Humanoid:TakeDamage(11)
			wait(0.100)
			Active = true
		end
	end)
end)

I hope this solves your issue and I wish a nice day!

(I also added RCPM which stands for RaycastParameters and they basically allow you to give your raycast certain parameters - like for example excluding the shooters character from the raycast so that you never accidentally shoot yourself!)

2 Likes

I’m getting a few errors though

Workspace.Tool.Script:32: attempt to concatenate string with Vector3

Everything else seems fine! Thank you

This is the erorr source! Fix it by doing:

print("Distance: "..Distance.X..","..Distance.Y..","..Distance.Z)
print("Dimensions: "..NewPart.Size.X..","..NewPart.Size.Y..","..NewPart.Size.Z)

(sorry for the mishap)

2 Likes

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