Can someone help on how to change the position of an part where the mouse is pointing

hi ive tried everything i know and it didnt work it only spawn on the origin which it 0,0,0 and i dont know how to do pls help me
here is my script:
local Players = game.Players.LocalPlayer
local mouse = Players:GetMouse()
local UIS = game:GetService(“UserInputService”)
local TwSer = game:GetService(“TweenService”)
local spherepos = mouse.Hit.p

UIS.InputBegan:Connect(function(Input)
		
	if Input.KeyCode == Enum.KeyCode.F then
			
		local Sphere = Instance.new("Part")
		
		Sphere.Name = "ROOM"
		Sphere.Transparency = 0.85
		Sphere.BrickColor = BrickColor.new("Electric blue")
		Sphere.Shape = Enum.PartType.Ball
		Sphere.Material = Enum.Material.Neon
		Sphere.Parent = game.Workspace
		Sphere.Anchored = true
		Sphere.CanCollide = false
		Sphere.Position = Vector3.new(spherepos)
		
		local TwInfo = TweenInfo.new(2,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,0,false,0)
		
		local SphereInfo = {
		Size = Vector3.new(200,200,200);
		}
		
		local Room = TwSer:Create(Sphere,TwInfo,SphereInfo)
		
		Room:Play()
		wait(10)
		
		local TwInfo1 = TweenInfo.new(2,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,0,false,0)
		
		local SphereInfo1 = {
		Size = Vector3.new(1,1,1);
		}
		
		local Room1 = TwSer:Create(Sphere,TwInfo1,SphereInfo1)
		
		Room1:Play()
		wait(2)
		
		game.Workspace.ROOM:Destroy()
	end
end)

thx in advance =)

The position of the mouse you stored as a variable doesn’t change, it remains the same as the moment you assigned it. You should move the variable and include it in the if-statement. Or alternatively, since I don’t see any other uses of the variable in the code you provided, you could just directly assign the position of the mouse to the position of the part.

Also, if I may add, you should use CFrame instead of the Position property:

Sphere.CFrame = CFrame.new(mouse.Hit.p)

EDIT: I also have another comment, instead of yielding the script to destroy an object using wait() you could use the AddItem() method of the Debris service.

.

2 Likes

Did not work, ive tried what you said and it did not work =(

EDIT: it worked now thx i forgot the sphere.position and ive do it TYSM

make sure you use a local script. Local scripts are ran on the client, Scripts are ran on the server. Local scripts have access to mouse positions, while scripts, do not.

1 Like