Why does my part not position correctly?

So I have a script to spawn a block infront of me but it doesnt seem to always be infront
When it is not anchored then it works most of the time but when you become anchored the part is placed in a position thats not infront of the character. I would like to know how I would fix this?

Server script

local frozen = false
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player)
	if frozen == false then
		frozen = true
		local part = Instance.new("Part",workspace)
		part.Anchored = true
		part.CFrame = player.Character.HumanoidRootPart.CFrame + player.Character.HumanoidRootPart.CFrame.LookVector * 3 
		part.CanCollide = false
		player.Character.HumanoidRootPart.Anchored = true
		player.Character.Humanoid.PlatformStand = true
	else
		frozen = false
		player.Character.HumanoidRootPart.Anchored = false	
		player.Character.Humanoid.PlatformStand = false
	end
end)

Local script

local uis = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local event = game.ReplicatedStorage.RemoteEvent

uis.InputBegan:Connect(function(input,istyping)
	if not istyping then
		if input.KeyCode == Enum.KeyCode.E then
			event:FireServer()
		end
	end
end)

Where exactly is this part being teleported to, when it does not work?

infront of the player.
It doesnt work properly when players move at any point. For example if I jump its going to be below the player and if I move the part will be inside of the player but if the player isn’t anchored when this happens it’s perfectly fine.

I am testing the scripts in a project file now, and I don’t see what your problem is? It is instancing a part infront of the player, which… is what you want. Correct me if I am wrong

image

Its not really infront of the player

Avoid using the second parameter of Instance.new, like in this line:

local part = Instance.new("Part",workspace)

Instead, parent the part to the desired parent after setting it up entirely. In your case, it’d be after the part.CanCollide = false line.

part.CanCollide = false
part.Parent = workspace

Using the second parameter while instantiating an object has performance issues and might possibly be the issue in your case.

1 Like

image It is for me lmao

1 Like

Here is the article if you want to read more into it

Try jumping or moving around. That’s the problem I have

1 Like

Also not the problem I have. I just edited the script and it didn’t change.
Also that was talking about performance issues which I don’t really have.

1 Like

They are minor optimisation changes, which I would highly recommend you keep and use in the future. Regarding the moving around situation: this part was created whilst jumping in the air (it looks like it should be in the correct placement, for when I was jumping in the air) image

Please try and go into more detail about what the actual problem is, seeming as I am failing to replicate it whilst using the same scripts.

Were you anchored in place? That’s what the script does. I said it kind of works perfectly fine if you don’t use anchor but I kind of want to be able to freeze a character in place.

It might be just lag on my end I don’t know

I understand what you mean now, thanks, however I have absolutely no explanation for why this is happening?? It is so weird.

I am going to try and investigate what the problem is, and when I get a solution, I will post it right here.
I have no clue to why this wouldn’t work. Anyone else want to give it a shot?

I found while testing that this is because of a server and client disagreement. Test and jump and press E so it looks like the part is not in front of the player. Then switch to server view.
image

Theoretically, if your ping was 0 then this wouldn’t happen.

The placement of the part does look correct to other players on the server. technically nothing is wrong

So the problem is because the client takes too long to send the position of HumanoidRootPart to the server and there is no solution. Maybe in 8 years when we all have extremely fast 6G internet then this will be fixed.

1 Like

It might be because when you anchor the player, and the player is in shiftlock, I think they can still rotate their HumanoidRootPart

hmm well having 0 ping is unrealistic and will never happen
maybe I can make the part invisible from client and make a new part as well to display it

1 Like

Just anchor the whole character. It prevents them from turning.

This might not change anything, but try setting the HumanoidRootPart to anchored before the part is instanced

already did but it doesn’t change anything but yeah as stated above it’s only a problem on the local side but on the server side it looks fine.

1 Like