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.
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)