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

How do i make a part's position a little away from our view but in our view?

> Mods and admins, the title is all about the desc... What is the matter???

Question is title.

So i have a problem, this is the part that it runs and doesn't work:

function GetNewAxisX(pl)
	if pl~=nil then
		if pl.Character then
			if pl.Character:findFirstChild('Head')~=nil then
				if pl.Character:findFirstChild('Head'):IsA('BasePart') then
					return pl.Character.Head.Rotation.Z /  pl.Character.Head.Rotation.X
				end
			end
		end
	end
end

Before you ask... YES, Variable 'Part' is a part, this is a script (Localscript isn't the problem).

The script is supposed to when we mouselock it appears in-front of our head (even when were not mouselocking)

any help?

1 answer

✓ Accepted answer

This is a site for people to help you - they may help with things you didn't think needed to be improved.

Nonetheless, style is very important for keeping your code maintainable, clean, and pretty. You can choose to disregard the suggestions, but keep them in mind because at times they will make code much nicer.

#Style Suggestions

~= nil is redundant and unnecessary.

It (in my opinion) is more intuitive / simpler to say if pl.Character then, etc.


Also, we know that Head is in fact a child of pl.Character so we don't need to use FindFirstChild the second time. We can probably assume that if Head is in a player's Character, it is in fact a part, so that can be probably be removed.


FindFirstChild ought to be uppercase according to ROBLOX's current stance on naming.

function GetNewAxisX(pl)
	if pl then
		if pl.Character then
			if pl.Character:FindFirstChild('Head') then
				if pl.Character.Head:IsA('BasePart') then -- Probably not necessary
					...

If you want the direction that the player is looking, just use its CFrame's lookVector (no weird stuff with Rotation)

return pl.Character.Head.CFrame.lookVector
-- A unit vector in the direction they are looking

If you want components of it, request its .x, .y, and .z properties (though usually code will be more elegant when phrased entirely in vectors without referring to the components)

2 comments

0

Actually, findFirstChild always worked for me and still works, and i used 'if head isnt nil' because this is a script that always runs that function, wich means if theres no detector and player dies the script would fully break... Anyway thank you for helping!

0

findFirstChild works, it's just not consistent with everything else, so it's a bit prettier to switch. The check for the existence of Head is a good idea -- you just don't need it the second time, and you probably don't need the BasePart check.

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