Raycast Module Offset Problem

Hello, I made a teleporter and I have a problem when I go through the tp the raycast goes back even though the attachment is still at the handle.
Video:
robloxapp-20230601-1808347.wmv (1.1 MB)

Teleport Script
local Modules = game:GetService("ReplicatedStorage"):WaitForChild("Client_Modules")
local Zone = require(Modules.Zone)

local container = script.Parent
local zone = Zone.new(container)

local debounce = {}
local cooldown = 1
local range = 9

function getSurface(trigger, rootPart, hit)
	local params = RaycastParams.new()
	params.FilterDescendantsInstances = {trigger}
	params.FilterType = Enum.RaycastFilterType.Include
	
	local surface = nil
	
	local distance = (trigger.Position - rootPart.Position).Magnitude
	
	local direction = (trigger.Position - rootPart.Position).Unit * math.ceil(distance + 3)
	local startPos = rootPart.Position
	
	local result = workspace:Raycast(startPos, direction, params)
	
	if result then
		local normal = result.Normal
		surface = normal
	end
	
	return surface
end

function Touched(hit)
	if hit.Parent:FindFirstChild("Humanoid") and not debounce[hit.Parent] then
		debounce[hit.Parent] = true

		local character = hit.Parent
		local rootPart = character:WaitForChild("HumanoidRootPart")

		local surface = getSurface(container, rootPart, hit)

		if surface then
			rootPart.Position = rootPart.Position + script.Parent.CFrame.LookVector * surface.Z * range
		end
		task.wait(cooldown)
		debounce[character] = nil
	end
end

container.Touched:Connect(Touched)

Module:

Thank you.

just create the raycast module yourself personally I wouldnt recomment using that module. Here is a sample of what I do to accomplish simillar result and can be done client or server.

for _, attachment in ipairs(attachments) do
				if attachment:IsA('Attachment') then
					local lastPosition = attachment.WorldPosition
					local startTime = tick()
					spawn(function()
						while tick() - startTime <= duration do
							local currentPosition = attachment.WorldPosition
							local raycastParams = RaycastParams.new()
							raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
							raycastParams.FilterDescendantsInstances = {tool, char}

							local direction = (currentPosition - lastPosition).Unit * 1 -- extend the raycast as far as possible

							local raycastResult = workspace:Raycast(lastPosition, direction, raycastParams)

							-- If the raycast didn't hit anything, we'll use the end of the raycast as the position
							local hitPosition = raycastResult and raycastResult.Position or (lastPosition + direction)

							local trailPart = Instance.new("Part")
							trailPart.Anchored = true
							trailPart.CanCollide = false
							trailPart.Size = Vector3.new(.1, .1, (hitPosition - lastPosition).Magnitude)
							trailPart.CFrame = CFrame.lookAt(lastPosition, hitPosition) * CFrame.new(0, 0, -trailPart.Size.Z / 2)
							trailPart.Parent = workspace
							trailPart.Color = Color3.fromRGB(255, 0, 4)
							if raycastResult then
								if raycastResult.Instance then
									if raycastResult.Instance.Parent then
										if raycastResult.Instance.Parent:FindFirstChild('Humanoid') then
											if osTouched < os.time() - .5 then
												osTouched = os.time()
												raycastResult.Instance.Parent.Humanoid:TakeDamage(33)

											end
										end
									end
								end
							end
										

							Debris:AddItem(trailPart, 0.5)

							lastPosition = currentPosition
							RunService.Heartbeat:Wait()
						end
					end)
					
				end
			end
1 Like

What are the benefits of using my own module rather than the raycast module. The raycast module seems to have more optimized.

I haven’t compared them but I would think mine has less calculations the lines just look less rounded but I dont like them on anyway its for preview

1 Like

(7) Zombie Infection Game - Roblox i got some tools using the exact function on some random game i stopped building

Just use whichever one works for you. If the script above works, and the raycast module doesn’t, then…

1 Like

I tried your script and it worked, but it feels less responsive/less accurate? But, I want to try investigate the module to see if I can actually find the problem. Thanks for the help if I can’t find a solution I’ll give you the solution. Thanks again!

The script i send you has to be in a runservice.Stepped:Wait() loop maybe thats the issue it cant be in wait() maybe that is it compare it to the one in my game it feels responsive there so im not sure u probably using wait()

When I try this the game just freezes. I’ll mark you as solution because I tried the latest update and it doesn’t work. Thank you so much!

not sure is it the same on the game i sent ?? and dont know make sure u run the attachment for i ,v loop in stepped loop or heartbeat loop then i guess and u could change inside from heartbeat to stepped possibly

1 Like

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