How to make an attachment where you click on the screen (Using Ray casting) (SOLVED)

I have been stuck with this for a while. This is my newest script.

script.Parent.Fire.OnServerEvent:Connect(function(player,mousePos)
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {player.Character}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

	local raycastResult = workspace:Raycast(script.Parent.Handle.Position, (mousePos - script.Parent.Handle.Position) * 300, raycastParams)

	if raycastResult then
		local hitPart = raycastResult.Instance
		local hitPosition = raycastResult.Position

		local attach = Instance.new("Attachment")
		attach.CFrame = CFrame.new(hitPosition)
		attach.Parent = hitPart

		print("Hit Position:", hitPosition)
		print("Attachment Position:", attach.WorldPosition)

		local balloon = game.ReplicatedStorage.Balloon:Clone()
		balloon.Parent = workspace
		balloon:SetPrimaryPartCFrame(CFrame.new(hitPosition))
		balloon.RopeConstraint.Attachment1 = attach

		local model = hitPart:FindFirstAncestorOfClass("Model")
		if model then
			local humanoid = model:FindFirstChildOfClass("Humanoid")
			if humanoid then
				humanoid:TakeDamage(30)
			end
		end
	end
end)

It works when i just click on the baseplate or something anchored but not when i click on something anchored. This the hit position and attachment position for an anchored part 06:56:39.499 Hit Position: -1.3556596040725708, 0, -10.056815147399902 - Server - Server2.0:16
06:56:39.499 Attachment Position: -1.3556596040725708, -10, -10.056815147399902 - Server - Server2.0:17 and here is an unanchored part 06:56:58.299 Hit Position: -29.928237915039062, 3.2020673751831055, -39.248931884765625 - Server - Server2.0:16
06:56:58.299 Attachment Position: -70.52479553222656, 11.900918960571289, -95.80228424072266 - Server - Server2.0:17

Can someone help me? What I want to accomplish is a gun and wherever you click/shoot an attachment will spawn/be created there and then I will attach a balloon to that attachment (I can do the balloon thing with a simple rope constraint) Any and all help is appreciated! It works when it is anchored but not on unanchored objects

Attachment.CFrame is relative to the parent part of the attachment. try setting its WorldCFrame instead

1 Like

I have already tried this but i will try it again (sorry for late reply i think we are in different time zones)

I just tried it. it did not work when I clicked on an object it put the object in the center of the object. Thanks for trying EDIT: I just re arranged the script a bit and it worked!

attachment.Position = Balloon.CFrame:ToObjectSpace(mousePos).Position

1 Like

Did you use chatGPT to answer to this question because :ToObjectSpace isn’t a valid function :confused:

Next time do your own research before you accuse me of such ridiculous things, maybe you should learn a thing or two.

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