How to make a block spawn in the same position when moving

Basically, I don’t know how I can fix this.

When I jump the part is always on the wrong spot and when I move it’s also on the wrong spot.
I want the part to be directly in front of the character even while moving

Server script:

local event = game.ReplicatedStorage.Event

event.OnServerEvent:Connect(function(player)
	if player.Character then
		local part = Instance.new("Part",workspace)
		part.Size = Vector3.new(1,1,1)
		part.Anchored = true
		part.CFrame = player.Character.HumanoidRootPart.CFrame + player.Character.HumanoidRootPart.CFrame.LookVector * 3
	end
end)

Local Script:

local event = game.ReplicatedStorage.Event
local uis = game:GetService("UserInputService")

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

I would appreciate any help thanks!

2 Likes

Try this perhaps? Might be due to how you’re ordering it

part.CFrame = (player.Character.HumanoidRootPart.CFrame + player.Character.HumanoidRootPart.CFrame.LookVector) * 3
1 Like

Nope doesn’t do anything different. Wait I can tell you that’s wrong because I’m trying to multiply the look vector to get 3 studs infront of the player not 3 * all cframes

Wait I misread the post my apologies

You could do this with a simple loop I presume?

local event = game.ReplicatedStorage.Event
local RunService = game:GetService("RunService")

event.OnServerEvent:Connect(function(player)
	if player.Character and not workspace:FindFirstChild(player.Name.."'s Part") then
		local part = Instance.new("Part",workspace)
		part.Size = Vector3.new(1,1,1)
		part.Anchored = true
        part.Name = player.Name.."'s Part"

        for Loop = 1, 500 do
		    part.CFrame = player.Character.HumanoidRootPart.CFrame + player.Character.HumanoidRootPart.CFrame.LookVector * 3
            RunService.Heartbeat:Wait()
        end
        part:Destroy()
	end
end)

I added in an additional feature where the Part would not clone if it was called multiple times, it should only work once

1 Like

Not a loop but I want it to spawn in front of the player

Let’s say you ran my code right

It works perfectly fine if you aren’t moving it spawns a block directly in front of you

but jump or move around. The block is always a bit off. So I was wondering why it was like that

Oh I see

I think this is just due to the HumanoidRootPart having an interactive movement, which keeps changing its position cause it’s still calculating where you need to set its Position while at the same time you’re moving it

Try this then:

local event = game.ReplicatedStorage.Event

event.OnServerEvent:Connect(function(player)
	if player.Character then
		local part = Instance.new("Part",workspace)
		part.Size = Vector3.new(1,1,1)
		part.Anchored = true
		
		if player.Character.Humanoid.MoveDirection.Magnitude == 0 then
			part.CFrame = player.Character.HumanoidRootPart.CFrame + player.Character.HumanoidRootPart.CFrame.LookVector * 5
		else
			part.CFrame = player.Character.HumanoidRootPart.CFrame + player.Character.HumanoidRootPart.CFrame.LookVector * 10
		end
	end
end)