Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would you make a mesh float several studs away from the players face?

I am making an object pick-up system for my game. Right now, everything is working, except for keeping the mesh in front of the players face. This is similar to the game "Murder"s pick-up system. I also want this to be serverside. Here is my code:

Client-Side Code:

game:GetService("UserInputService").InputBegan:connect(function(input)
    if input.KeyCode == Enum.KeyCode.E then
    
    game.ReplicatedStorage.events.PickUp:FireServer()
	print("player picked up")
	
    end
end)

game:GetService("UserInputService").InputEnded:connect(function(input)
    if input.KeyCode == Enum.KeyCode.E then
    
    game.ReplicatedStorage.events.PlaceDown:FireServer()
	print("player put down")

    end
end)

Server-Side Code:

local stop = 0
local x
local y
local z

game.ReplicatedStorage.events.PickUp.OnServerEvent:Connect(function(player)
	local character = player.Character
	local magnitude = (character.Head.Position - script.Parent.Position).Magnitude
	
	
	
	if magnitude <= 5 then
		while stop == 0 do
			x = character.Head.Position.X + 5  -- this is where i need help. i can make it stay with the player, but not in front of their face.
			y = character.Head.Position.Y
			z = character.Head.Position.Z + 5
			
			script.Parent.Position = Vector3.new(x,y,z)
			
			wait(0.05)
			
		end
	end
	
end)

game.ReplicatedStorage.events.PlaceDown.OnServerEvent:Connect(function()
	stop = 1
	wait(0.5)
	stop = 0
end)


As you may be able to tell, my code right now just moves it to the players X and Z + 5. Hopefully, there is a fix for this. Thanks!

1 answer

✓ Accepted answer

You can fix this with CFrame.

Simply replace these lines

x = character.Head.Position.X + 5
y = character.Head.Position.Y
z = character.Head.Position.Z + 5
script.Parent.Position = Vector3.new(x,y,z)

With this line to get your desired result (hopefully!)

script.Parent.CFrame= character.Head.CFrame * CFrame.new(5,0,5)

Hope this helped :D

5 comments

0

This is so close! Except that it appears to put the mesh at the side of my head. I also tested with a normal part, and still the same result. Thanks!

0

You want it directly in front of the player's head? Just change the CFrame.new(5,0,5) to CFrame.new(0,0,5) and I think that'll fix it

0

If that doesn't work try CFrame.new(5,0,0) ... I forget which is which lol

0

Yeah, it still stays at the side with both.

0

OK I got it. It is CFrame.new(0,0,-5)

This discussion is preserved as it was. New answers, comments, and votes are closed.