Help with teleportation ability

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I have a script in a tool that allows the player to teleport to their mouse click position when pressing T. no i don’t want mobile support.
  2. What is the issue? Include screenshots / videos if possible!
    i’ve recently added “areas” into my game, where when you are in an area, it locally changes your Lighting.Atmosphere properties to something else. these “areas” are basically giant CanCollide off invisible parts when touching will toggle the property change. when using the teleport ability, no matter where you click, you just get teleported to the centre of the area.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    i cannot change the CanTouch or CanCollide property in the areas, as that would break the scripts. i am looking for maybe an attribute where if a physical instance has it, the teleport would go through it. code is below. any help appriciated!!
    btw the code is a local script in the tool.
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
local tool = script.Parent
local plr = game:GetService('Players').LocalPlayer
local mouse = plr:GetMouse()

mouse.KeyDown:Connect(function(key) --runs the function when a key is pressed
	key = string.lower(key)
	if key == "t" and tool.Parent == plr.Character then --only when the key "t" is pressed it will run

	tool.Parent:moveTo(mouse.hit.Position)
	
	
	end
end)


I hope this is what you are looking for!

local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local CollectionService = game:GetService("CollectionService")

local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()

local Raycastparams = RaycastParams.new()
Raycastparams.FilterType = Enum.RaycastFilterType.Exclude

local MaxTeleportDistance = 100

local PlayerMouse = Player:GetMouse()

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.T then
		Raycastparams.FilterDescendantsInstances = CollectionService:GetTagged("TAG_NAME_HERE")
		
		local mouseHit = PlayerMouse.Hit
		local raycastResult = workspace:Raycast(PlayerMouse.Origin.Position, PlayerMouse.Origin.LookVector * MaxTeleportDistance, Raycastparams)

		if raycastResult then
			local hitPosition = raycastResult.Position
			Character:SetPrimaryPartCFrame(CFrame.new(hitPosition))
		end
	end
end)

I think you’re looking for raycasts with RespectCanCollide on.

-- this goes to the other variables on top
local raycastParams = RaycastParams.new()
raycastParams.RespectCanCollide = true

-- this goes in your input function
local raycast = workspace:Raycast(mouse.UnitRay.Origin, mouse.UnitRay.Direction*1000, raycastParams)
-- if raycast hit something
if raycast then
	tool.Parent:MoveTo(raycast.Position)
end

This code is assuming you’re using a client script. You’re gonna have to do the teleporting on the server side though, which you also didin’t do in your previous script. But that wasn’t part of the original question, and I’m sure you can figure it out yourself.

wow. didn’t expect to get a working answer first response. im just wondering, could you tell me how the script works? i just want to have a good look at this new script, and make sure it doesn’t create any additional bugs for my game.

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