Help with positioning part

I’m pretty new to programming and I wish to seek help with some code i’m trying to write. I wish to have a part follow behind a player but I can’t seem to figure out the issue since the output sees no error.

--localscript

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local RemoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")

RemoteEvent:FireServer(Vector3.new(0,0,6))

--server

Part = workspace.Part

 Player = game.Players.LocalPlayer

 C = Player.Character

 ReplicatedStorage = game:GetService("ReplicatedStorage")

 RemoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")

local function PartPositioning(NewPosition)
	
	local Humanoid = C.HumanoidRootPart
	
	Part.Position = Humanoid.Position + NewPosition
	
	
end


--Calls on function when player fires the remoteEvent

RemoteEvent.OnServerEvent:Connect(PartPositioning)

What happens when you run the script? Does the part appear but in the wrong position? Does it not appear at all? Add more detail, so people can help you more easily.

Just so you know, you can’t use game.Players.LocalPlayer in a server script.

You can only use it in Local Script, in other words, It can only be used to refer the player in a local script. Since a Server Script controls the game and is not a player, you cannot use game.Players.LocalPlayer in a server script.

Oh uh basically nothing happens, and nothing is shown in the input.

Oh thank you I never knew that.

I think it may be this line that messes things up, LocalPlayer has a nil value when called in a regular script, try changing it to something else.

As already mentioned, you cannot access LocalPlayer from the server. If you wanted the player object you would have to directly specify which player (i.e. game.Players:FindFirstChild("Player Name")), or put the script inside a function binded to an event which gives you a Player object, such as:

game.Players.PlayerAdded:Connect(function(player)
    -- You can use the player object here
    local C = player.Character
end)

Another thing to note is on the server, when you bind a function to the OnServerEvent event, the first argument of the function is always the player which fired the event. For example, you should change the PartPositioning function to take the player as the first parameter, like so:

local function PartPositioning(Player, NewPosition)
    -- code
end

This ties in nicely because now you have the player object.


In conclusion, you could approach this problem like this:
LocalScript:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")

RemoteEvent:FireServer(Vector3.new(0,0,6))

ServerScript:

ReplicatedStorage = game:GetService("ReplicatedStorage")
RemoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")

local Part = game.Workspace.Part

function PartPositioning(Player, Position)
    local RootPart = Player.Character.PrimaryPart
    Part.Position = RootPart.Position + Position
end

RemoteEvent.OnServerEvent:Connect(PartPositioning)

Hm, the problem still persist. I look in the output and see no errors but nothing seems to happen.

Is there a place where I should put the script specifically? or am I missing something?

Here is what I tried to change.(I also added some stuff but I don’t know if that effects)


--local script
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local RemoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")
		
RemoteEvent:FireServer(Vector3.new(0,20,0))

--script

 ReplicatedStorage = game:GetService("ReplicatedStorage")

 RemoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")

local Part = Instance.new("Part")

 function PartPositioning(Player,NewPosition)
	
	local RootPart = Player.Character.PrimaryPart
	
	Part.Anchored = true
	
	Part.Position = RootPart.Position + NewPosition
	
	Part.CanCollide = false
	
	Part.Size = Vector3.new(10,10,10)
	
	
end


--Calls on function when player fires the remoteEvent

RemoteEvent.OnServerEvent:Connect(PartPositioning)

Try adding print() statements in order to debug, for example:

function PartPositioning(Player, NewPosition)
        print("Reached PartPositioning function!")
	local RootPart = Player.Character.PrimaryPart
	Part.Anchored = true
	Part.Position = RootPart.Position + NewPosition
	Part.CanCollide = false
	Part.Size = Vector3.new(10,10,10)	
end

Also ensure the LocalScript is not in Workspace, because LocalScripts do not run if they are in Workspace.

What does it mean when nothing prints?

That means that the function is never called, so most likely the RemoteEvent is not being fired.

Is the part that you instanced parented to the workspace? :thinking:

yes, I do have the Part parented to the workspace.

So you see the part but it doesn’t move?

The part doesn’t appear at all for some weird reason.

That means it isn’t parented you need to do Part.Parent = workspace in the function

I did uh local Part = Instance.new(“Part”,workspace)
does that achieve the same thing?

I didn’t saw that in the script well yeah you can also do that

Yes, that achieves the same thing but it is better to parent on a new line.

local Part = Instance.new("Part")
Part.Parent = game.Workspace

is better than

local Part = Instance.new("Part", game.Workspace)

Here are some reasons why: PSA: Don't use Instance.new() with parent argument

1 Like

Hm, I would assume it won’t work because of where I placed it, I put the local script is StarterPlayerScripts

I also put the regular script in the remote function itself