Orientation keeps changing for some reason

Ok so this script copies a killbrick into the characters head, then it adjusts it so that the killbrick is facing the characters head. This is not happening in my script, the orientation keeps changing for some reason and it is not accurate

Script:


Players.PlayerAdded:Connect(function(player)
	player:GetPropertyChangedSignal("Team"):Connect(function()
		if player.Team == KillerTeam then
			if player.Team == KillerTeam then
				local character = player.Character

				if character then
					player.CameraMode = "LockFirstPerson"
					local kb = KillBrick:Clone()
					kb.Parent = character.Head
					kb.CFrame = (character.Head.CFrame + Vector3.new(0,0,-2))
					kb.Orientation = Vector3.new (90,0,0)
					local weld = Instance.new("WeldConstraint")
					weld.Parent = character.Head
					weld.Part0 = kb
					weld.Part1 = character.Head

					kb.IsInUse.Value = true
				end

				Connections[player] = player.CharacterAdded:Connect(function(character)
					local kb = KillBrick:Clone()
					kb.Parent = character.Head
					kb.CFrame = (character.Head.CFrame + Vector3.new(0,0,2))
					kb.Orientation = Vector3.new (90,0,0)
					local weld = Instance.new("WeldConstraint")
					kb.Name = "killerBlock"
					weld.Name = "WeldThingy"
					weld.Parent = character.Head
					weld.Part0 = kb
					weld.Part1 = character.Head
					player.CameraMode = "LockFirstPerson"

					kb.IsInUse.Value = true

				end)
			end
		else
			local connection = Connections[player]

			if connection then
				connection:Disconnect()
				event.OnServerEvent:Connect(function(player)
					local char = player.character
					char.killerBlock:Destroy()
					char.WeldThingy:Destroy()
					char:GetPlayerFromCharacter().CameraMode = "Classic"
					player:LoadCharacter()
				end)
			end

			end
		--//Reset the character back to normal here
		end)
end)
1 Like

are you trying to place the killbrick in the exact position of the players head?

also you don’t need this second if your checking it 2 times in row here

Yes, I am trying to do, I forgot to remove that. My fault.

Players.PlayerAdded:Connect(function(player)
	player:GetPropertyChangedSignal("Team"):Connect(function()
		if player.Team == KillerTeam then
				local character = player.Character

				if character then
					player.CameraMode = "LockFirstPerson"
					local kb = KillBrick:Clone()
					kb.Parent = character.Head
					kb.CFrame = (character.Head.CFrame + Vector3.new(0,0,-2))
					kb.Orientation = Vector3.new (90,0,0)
					local weld = Instance.new("WeldConstraint")
					weld.Parent = character.Head
					weld.Part0 = kb
					weld.Part1 = character.Head

					kb.IsInUse.Value = true
				end

				Connections[player] = player.CharacterAdded:Connect(function(character)
					local kb = KillBrick:Clone()
					kb.Parent = character.Head
					kb.CFrame = (character.Head.CFrame + Vector3.new(0,0,2))
					kb.Orientation = Vector3.new (90,0,0)
					local weld = Instance.new("WeldConstraint")
					kb.Name = "killerBlock"
					weld.Name = "WeldThingy"
					weld.Parent = character.Head
					weld.Part0 = kb
					weld.Part1 = character.Head
					player.CameraMode = "LockFirstPerson"

					kb.IsInUse.Value = true

				end)
		else
			local connection = Connections[player]

			if connection then
				connection:Disconnect()
				event.OnServerEvent:Connect(function(player)
					local char = player.character
					char.killerBlock:Destroy()
					char.WeldThingy:Destroy()
					char:GetPlayerFromCharacter().CameraMode = "Classic"
					player:LoadCharacter()
				end)
			end

			end
		--//Reset the character back to normal here
		end)
end)

I am also trying to make sure the orientation is facing the players head

maybe try just setting straight and always parent after setting up your properties etc
i know with welds you may have to have the part parented into the workspace first thoguh

local kb = KillBrick:Clone()
kb.CFrame = character.Head.CFrame  -- maybe try the exact position and rotation by just setting the heads cframe
--kb.Orientation = Vector3.new (90,0,0)
local weld = Instance.new("WeldConstraint")
weld.Part0 = kb
weld.Part1 = character.Head
weld.Parent = character.Head

kb.Parent = character.Head  -- might want to parent last if this doesn't work with the weld parent right before the weld
1 Like

Also you may want to do local character = player.Character or player.CharacterAdded:Wait() since this is in a player added function to make sure the character is there

Is this a local or server script?

1 Like

This script is a server script inside ServerScriptService.

I tried the code you sent me and it put the killbrick on the players head but it was facing downwards.

ok you probably won’t be able to change the players camera to firstperson and also you may want to use CharacterAppearanceLoaded and WaitForChild(‘Head’) to make sure the character is fully loaded if not it may auto remove the killbrick when appearance loads in the character
and also want to make sure the head has loaded in with that wait

https://create.roblox.com/docs/reference/engine/classes/Player#CharacterAppearanceLoaded

both should face forwards unless you are rotating some how before the weld

is your part a mesh or something or just a plan part?

see this is with the code

815df56bdd52c88a76512d85b6313b1d

1 Like
kb.CFrame = (character.Head.CFrame + Vector3.new(0,0,-2))

I have this inside my code, doesn’t this do the same thing? (the -2 z is for it to be infront of the head.

Try not to change the orientation after setting the CFrame, instead use something like:

kb.CFrame = (character.Head.CFrame)
local offsetCFrame = kb.CFrame + Vector3.new(0,0,-2)
weld.C0 = offsetCFrame*CFrame.Angles(math.rad(90),0,0)

Edit: You have to set the angles of the WELD’s offset, not the CFrame of the object

1 Like

CFrames go over my head half the time but after like 5 edits the above should be helpful^

that will make it actually add to the cframe instead if you want to move it forward or backward you would need to add the look vector times the amount of studs like below which will move it forward like in the image

kb.CFrame = character.Head.CFrame + (character.Head.CFrame.LookVector * 2) 

if you want it behind the head then use -

a6cd5e457a2ba3ff9b302d9f5d933103

forgot to post this

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.