Is there any way to make a part hover infront of player?

I have this game where there is a custom equipping of a tool where you have to hold click to pick it up or right click to drop it depending on what it is. I want it to fit within the character’s proximity and the mouse. I’ve tried as much as I could think of to fix this, but I can’t.
Is there any fix to it?

1 Like

Please include the code which you are trying to use, so that we will be better equipped to help you with your problem.

1 Like

This is the part I’m focusing on.

repeat selectionbox.Visible = true; pos = mouse.hit / (game.Players.LocalPlayer.Character:WaitForChild(“Head”.CFrame); event:FireServer(model, hum, pos, tool, false); wait() until grabbingitem ~= true

I’ve been trying a lot to get it to fit, but I can’t.

1 Like

Please encode your code using either

~~~~~
– # code
~~~~~
or
```lua
– # code
```

Without the escape (I used backslash to escape the formatting)

repeat 
    selectionbox.Visible = true; 
    pos = mouse.hit / (game.Players.LocalPlayer.Character:WaitForChild(“Head”.CFrame);    event:FireServer(model, hum, pos, tool, false);
    wait() 
until grabbingitem ~= true

However this is not the full code, you did not show where you define grabbingitem, tool, ect so I am not able to help you any further until you provide all the relevant code.

2 Likes

It’s a bit unfinished, but this is all the code.

local grabbingitem = false
local event = game:GetService("ReplicatedStorage"):WaitForChild("Transfer")

game:GetService("RunService").RenderStepped:Connect(function()
	local mouse = game.Players.LocalPlayer:GetMouse()
	local target = mouse.Target or nil
	if grabbingitem ~= true then
		if target ~= nil then
			if target.Parent:FindFirstChildOfClass("SelectionBox") then
				local selectionbox = mouse.Target.Parent:FindFirstChildOfClass("SelectionBox")
				selectionbox.Visible = true
				local kc = mouse.Button1Down:Connect(function()
					grabbingitem = true
				end)
				repeat target = mouse.Target; wait() until target ~= selectionbox or grabbingitem
				kc:Disconnect()
				if grabbingitem then
					if selectionbox:FindFirstAncestorOfClass("Tool") then
						local tool = selectionbox:FindFirstAncestorOfClass("Tool")
						local model = selectionbox:FindFirstAncestorOfClass("Model")
						local holdingmouse = true
						local mousedown = mouse.Button1Up:Connect(function()
							holdingmouse = false
						end)
						repeat selectionbox.Visible = true; wait() until holdingmouse ~= true
						mousedown:Disconnect()
						local letgo = mouse.Button2Up:Connect(function()
							grabbingitem = false
						end)
						local shoot = mouse.Button1Down:Connect(function()
							local hum = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
							local pos = mouse.hit
							event:FireServer(false, hum, pos, tool, true)
							tool:Activate()
							event:FireServer(false, hum, pos, tool, false)
						end)
						local hum = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
						local pos = mouse.hit
						repeat selectionbox.Visible = true; pos = mouse.hit / (game.Players.LocalPlayer.Character:WaitForChild("Head").CFrame); event:FireServer(model, hum, pos, tool, false); wait() until grabbingitem ~= true
						selectionbox.Visible = false
						letgo:Disconnect()
						shoot:Disconnect()
					end
				else
					grabbingitem = false
					selectionbox.Visible = false
				end
			end
		end
	end
end)
1 Like

Not completely sure if this is what you’re asking but this function gets a cframe between the head and the mouse target

local function getCFrame()
	local headCF = player.Character.Head.CFrame
	local mouseT = mouse.Target
	if (mouseT) then
		local lookCF = CFrame.new(headCF.p, mouseT.CFrame.p) -- Head CF looking at mouse
		local mag = (headCF.p - mouseT.CFrame.p).Magnitude -- Distance between mouse & head
		local CF = lookCF * CFrame.new(0, 0, -(mag/2)) -- Exact middle between mouse CFrame and head CFrame
		return CF
	end
end
2 Likes

There’s a really easy way to create a part which stays at the same position away from another part using attachments and constraints. Be sure to position the Part where you want it to be first and then use this.

local Ball = Player.Character.HumanoidRootPart --The part being attached to
local Part = game.Workspace.Part --The part being attached

local Proxy = Instance.new(“Part”)
Proxy.Parent, Proxy.Name = game.Workspace, “Proxy”
Proxy.Size = Vector3.new(1, 1, 1)
Proxy.CanCollide, Proxy.Massless = false, true
Proxy.Transparency = 1
Proxy.CFrame = CFrame.new(Ball.Position)

local Attachment0 = Instance.new(“Attachment”)
Attachment1 = Attachment0:Clone()
Attachment0.Parent, Attachment1.Parent = Proxy, Ball

local BallSocket = Instance.new(“BallSocketConstraint”)
BallSocket.Parent, BallSocket.Radius = Proxy, 0
BallSocket.Attachment0, BallSocket.Attachment1 = Attachment0, Attachment1

Part.CanCollide, Part.Massless = false, true
local WeldAttachment = Instance.new(“WeldConstraint”)
WeldAttachment.Parent = Proxy
WeldAttachment.Part0, WeldAttachment.Part1 = Proxy, Part

Code from:
https://devforum.roblox.com/t/attach-a-non-cancollide-part-to-a-ball-while-keeping-parts-orientation-relative-to-the-world-space-while-ball-rolls/212943/9

I tried testing this out and tried to modify it to set a model’s primary part (I forgot to mention that) and this happened.

https://gyazo.com/5c942b51f138b0fa5eb4ba1044f32401

1 Like

um ok lol, can I see the modified code?

1 Like
local grabbingitem = false
local event = game:GetService("ReplicatedStorage"):WaitForChild("Transfer")

game:GetService("RunService").RenderStepped:Connect(function()
	local mouse = game.Players.LocalPlayer:GetMouse()
	local target = mouse.Target or nil
	if grabbingitem ~= true then
		if target ~= nil then
			if target.Parent:FindFirstChildOfClass("SelectionBox") then
				local selectionbox = mouse.Target.Parent:FindFirstChildOfClass("SelectionBox")
				selectionbox.Visible = true
				local kc = mouse.Button1Down:Connect(function()
					grabbingitem = true
				end)
				repeat target = mouse.Target; wait() until target ~= selectionbox or grabbingitem
				kc:Disconnect()
				if grabbingitem then
					if selectionbox:FindFirstAncestorOfClass("Tool") then
						local tool = selectionbox:FindFirstAncestorOfClass("Tool")
						local model = selectionbox:FindFirstAncestorOfClass("Model")
						local function getCFrame()
							local headCF = game.Players.LocalPlayer.Character.Head.CFrame
							local mouseT = model
							if (mouseT) then
								local lookCF = CFrame.new(headCF.p, mouseT.PrimaryPart.CFrame.p)
								local mag = (headCF.p - mouseT.PrimaryPart.CFrame.p).Magnitude
								local CF = lookCF * CFrame.new(0, 0, -(mag/2))
								return CF
							end
						end
						local holdingmouse = true
						local mousedown = mouse.Button1Up:Connect(function()
							holdingmouse = false
						end)
						repeat selectionbox.Visible = true; wait() until holdingmouse ~= true
						mousedown:Disconnect()
						local letgo = mouse.Button2Up:Connect(function()
							grabbingitem = false
						end)
						local shoot = mouse.Button1Down:Connect(function()
							local hum = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
							event:FireServer(false, getCFrame(), tool)
							wait()
						end)
						local hum = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
						local rot = game.Players.LocalPlayer.Character.PrimaryPart.CFrame
						repeat selectionbox.Visible = true; event:FireServer(model, getCFrame(), nil); wait() until grabbingitem ~= true
						selectionbox.Visible = false
						letgo:Disconnect()
						shoot:Disconnect()
					end
				else
					grabbingitem = false
					selectionbox.Visible = false
				end
			end
		end
	end
end)
1 Like

I’m confused. So do you want the model to just be a few studs away from the head? Where do you want the model to be?

1 Like

Yes. I want it to be atleast 7 studs away from the head.

Yes but where? Do you want it 7 studs away and pointing at the mouse? I don’t understand what you want the positioning to be.

1 Like

That is what I want.

1 Like

Just do this then.

local function getCFrame(object)
    if (object:IsA("Model")) then object = object.PrimaryPart end
    local headCF = player.Character.Head.CFrame
	local mouseT = mouse.Target
	if (mouseT) then
		local lookCF = CFrame.new(headCF.p, mouseT.CFrame.p)
		local CF = lookCF * CFrame.new(0, 0, -7) 
		return CF
	end
end

also why not use :BindToRenderStep()?

3 Likes

I’m a novice scripter so I don’t really know what to use, but thanks for your help!

2 Likes