Anchoring a part not working?

Hello! I made a script where it teleports a player to where their mouse aims at. The issue is, when they click their mouse, the part that shows where they’re gonna get teleported doesn’t stay in place. I’ve tried to search the forums, but couldn’t find anything. I’ve tried anchoring the part, but it’s not working.
(This is a localscript inside a frame in StarterGui)

local userinputservice = game:GetService("UserInputService")
local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()
local RunService = game:GetService("RunService")
local Frame = script.Parent

local part = Instance.new("Part", workspace)
part.CanCollide = false
part.Material = "SmoothPlastic"
part.Orientation = Vector3.new(0, 0, 90)
part.Color = Color3.fromRGB(34, 255, 0)
part.Transparency = 0.5
part.Size = Vector3.new(0.54, 2, 2)
local Mesh = Instance.new("SpecialMesh", part)
Mesh.MeshType = "Cylinder"

Mouse.TargetFilter = part

local stepped

stepped = RunService.RenderStepped:Connect(function()
	part.Position = Mouse.Hit.Position
end)

userinputservice.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		part.Anchored = true
	end
end)

Frame.Button.MouseButton1Click:Connect(function()
	Player.Character:MoveTo(Mouse.Hit.Position)
end)
1 Like

Where in the script do you get the UserInputService service?

Oops, forgot to get the whole script, lemme edit it. Edited!

I edited the topic, it shows the full code now.

Isn’t that the same thing???

I think I see the error. You are setting the part’s CanCollide to false, meaning it will fall through the world and eventually destroy because it is not anchored. If you set the part to Anchored in the beginning when you parent it, your issue should be fixed.

Works, but I’m moving the position of the part by RunService, so it moves regardless if it’s anchored or not.

You could also be attempting to set the part’s position to a nil value, such as if you were to hover your mouse into the sky/void, the script will either return an error, or move the part to a location that it gets destroyed.

The position property will still update regardless of if the part is anchored or not, so you need to stop setting the position in the RenderStepped connection if the part is anchored.

2 Likes

And how would I be able to do that? I don’t know how to stop RunService,

where you have “part.Position = Mouse.Hit.Position” you can wrap that into an if statement that checks the Anchored property of the part.

2 Likes

I did, but how do I stop the part from moving? It just checks if the anchored property is false.

stepped = RunService.RenderStepped:Connect(function()
if not part.Anchored then
part.Position = Mouse.Hit.Position
end
end)
2 Likes

That’s what I did, the code is the same, it’s still not stopping the part?

That’s quite odd because I just tried what I said in studio and it works fine.

1 Like
if part.Anchored == false then
		part.Position = Mouse.Hit.Position
	else
		
	end

My code, didn’t put anything in else yet.

I think the only option is to stop RunService, but I don’t know how, and I want the part to run again after the player got teleported.

so i’m not sure if this would work as i put it to together pretty quickly but here one way to do it

local userinputservice = game:GetService("UserInputService")
local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()
local RunService = game:GetService("RunService")
local Frame = script.Parent

local part = Instance.new("Part", workspace)
part.CanCollide = false
part.Material = "SmoothPlastic"
part.Orientation = Vector3.new(0, 0, 90)
part.Color = Color3.fromRGB(34, 255, 0)
part.Transparency = 0.5
part.Size = Vector3.new(0.54, 2, 2)
local Mesh = Instance.new("SpecialMesh", part)
Mesh.MeshType = "Cylinder"

Mouse.TargetFilter = part

local yeild = false -- will be used to break while loop
local pos = mouse.Hit.Position -- used for position to teleport to

local funtion move() -- used for part movement
	while wait(.01) do
		part.Position = Mouse.Hit.Position
		pos = Mouse.Hit.Position
		if yeild == true then
			yeild = false
			break 
		end
	end
end

move()--call function first time

userinputservice.InputBegan:Connect(function(input) -- is this to stop movement of part so you can teleport to that position?
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
	yeild = true -- stop loop
	end
end)

Frame.Button.MouseButton1Click:Connect(function()
	Player.Character:MoveTo(pos)
	move() -- call it to restart part movement
end)

I’m not probably going to reply for at least 10 more hours (sleep) so if it doesn’t work i wont be able to fix it until then

Oh, I actually fixed it myself, though it makes the movement laggy, I don’t mind.

The best way to improve the laggy part is just to make the while wait(.01) do into while wait() do that should help a bit