Issues with Vector3.new()

In my game I made a script for a door so when you click the handle, it plays an animation, fades to black and teleports you. It works. I also made a script so when the part is clicked (a note in this situation) it teleports 1 stud away from your head, allowing you to read it, and then if you move 1.5 studs or click on it again, it goes back. This script also works. But for some reason, when you go through a door, and then click on the note, it teleports nearly 3 studs too far- it’s rotated correctly to the players head though. I have never encountered this issue, and I have no idea why it happens (probably a fault on roblox’s side tbh).
I do know one thing though. Without the line:

player.Character.HumanoidRootPart.Position = Vector3.new(X, Y, Z)

The note works fine. Although it is what teleports the player so I can’t just delete it. This bug would probably get fixed if this line got replaced with a different one that does the same thing, except I do not know how to go about that. Anyways, here’s the scripts:

Door script:

local model = script.Parent
local SFX = model.Parent.SoundPART.open
local SFX1 = model.Parent.SoundPART.close
local SFX2 = model.Parent.SoundPART.locked
local Players = game:GetService("Players")

model.ClickDetector.MouseClick:Connect(function(player)
	local locked = script.Locked
	if not locked.Value then
		SFX:Play()
		model.Parent.AnimScript.Event:Fire()
		model.ClickDetector.MaxActivationDistance = 0
		local X, Y, Z = model.Parent.Out.Position.X, model.Parent.Out.Position.Y, model.Parent.Out.Position.Z
		local LP = Players:FindFirstChild(player.Name)
		for i = 1, 50 do
			LP.PlayerGui.Fade.FadeFrame.BackgroundTransparency -= 0.01
			SFX.Volume -= 0.025
			LP.PlayerGui.StaminaGui.Enabled = false
			wait()
			LP.PlayerGui.Fade.FadeFrame.BackgroundTransparency -= 0.01
		end
		wait(2)
		player.Character.HumanoidRootPart.Position = Vector3.new(X, Y, Z)
		SFX1:Play()
		wait(1.5)
		LP.PlayerGui.Fade.FadeFrame.Transparency = 1
		LP.PlayerGui.StaminaGui.Enabled = true
		model.ClickDetector.MaxActivationDistance = 5
		SFX.Volume = 1
	else
		SFX2:Play()
		model.ClickDetector.MaxActivationDistance = 0
		model.Parent.AnimScript.Event:Fire()
		wait(2)
		model.ClickDetector.MaxActivationDistance = 5
	end
end)

Note script:

local OriginalPosition = script.Parent.Position
local OriginalRotation = script.Parent.Rotation
local OriginalSBPS = script.Parent.PickUp.PlaybackSpeed
local OriginalSBPS1 = script.Parent.PutDown.PlaybackSpeed
local player = nil
local resetDistance = 1.5  -- Set the distance threshold for resetting the note

-- Function to reset the note
local function resetNote()
	local n = math.random(1, 2) -- These variables are in place to slightly randomize the SFX pitch.
	local n1 = math.random(1, 2)
	script.Parent.Position = OriginalPosition
	script.Parent.Rotation = OriginalRotation
	script.OnGround.Value = true
	if n1 == 1 then
		script.Parent.PutDown.PlaybackSpeed -= n / 21
	else
		script.Parent.PutDown.PlaybackSpeed += n / 20
	end
	script.Parent.PutDown:Play()
	wait(0.6)
	script.Parent.PutDown.PlaybackSpeed = OriginalSBPS1
end

-- Function to calculate distance between two Vector3 points
local function distance(point1, point2)
	return (point1 - point2).Magnitude
end

-- Connect the MouseClick event of the ClickDetector to a function
script.Parent.ClickDetector.MouseClick:Connect(function(clickedPlayer)
	-- Check if the player exists and has a character
	if clickedPlayer and clickedPlayer.Character then
		local n = math.random(1, 2) -- These variables are in place to slightly randomize the SFX pitch.
		local n1 = math.random(1, 2) 
		player = clickedPlayer
		script.Parent.ClickDetector.MaxActivationDistance = 0

		if script.OnGround.Value == true then
			script.OnGround.Value = false
			local headPosition = player.Character.Head.Position

			-- Calculate the direction vector from the part to the player's head
			local direction = headPosition - script.Parent.Position

			-- Calculate the rotation needed to face the player's head
			local rotation = CFrame.new(script.Parent.Position, headPosition).LookVector

			if n1 == 1 then
				script.Parent.PickUp.PlaybackSpeed -= n / 21
			else
				script.Parent.PickUp.PlaybackSpeed += n / 20
			end
			script.Parent.PickUp:Play()

			for i = 1, 10 do
				script.Parent.Rotation += Vector3.new(2,0,0)
				script.Parent.Position += Vector3.new(0,0.04,0)
				wait()
			end
			wait(0.61)
			script.Parent.PickUp.PlaybackSpeed = OriginalSBPS

			script.Parent.Position = headPosition - rotation * 1

			-- Set the rotation of the part to face the player's head
			script.Parent.CFrame = CFrame.new(script.Parent.Position, headPosition)

			-- Start checking for player movement
			local originalPlayerPosition = player.Character.HumanoidRootPart.Position

			-- Reset the note if the player moves beyond a certain distance
			spawn(function()
				while true do
					wait(0.2)  -- Check every second

					if player.Character and player.Character.HumanoidRootPart then
						local currentPlayerPosition = player.Character.HumanoidRootPart.Position
						local distanceMoved = distance(currentPlayerPosition, originalPlayerPosition)

						if distanceMoved >= resetDistance then
							if script.OnGround.Value then
								--print("On ground")
							else
								--print("In air")
								resetNote()
							end
							break
						end
					end
				end
			end)
		else
			resetNote()
		end
		script.Parent.ClickDetector.MaxActivationDistance = 5
	else
		error("Error: Player or player's character not found.")
	end
end)

I really don’t know what could be messed up with this, so any help with fixing this will be very appreciated.

1 Like

I don’t think I even need to read your script to know this is an issue with setting the character’s position. Try setting the HumanoidRootPart’s CFrame instead. Report to us if it works.

In addition to that, you can do Character:SetPrimaryPartCFrame() or Character:PivotTo() to set the CFrame of the HumanoidRootPart.

2 Likes

Thanks for the tip. I haven’t been able to try it out the last couple of days, but I have tested it now and it actually works.

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