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)
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.
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.
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