Here’s how to make the script work without changing any code at all.
Open Studio, and Play the game with a character.
Change the View to Server (from the Home tab)
In Explorer, open Workspace, then the model that has your name, and select the HumanoidRootPart.
Click and drag the HumanoidRootPart into Workspace.
The script in your post will now spawn the brick on the player.
Why does this happen?
local TargetPart = game.Workspace:WaitForChild("HumanoidRootPart")
This code will wait until there is a HumanoidRootPart
in Workspace. But it won’t wait until there is a model with a HumanoidRootPart in it! It quite literally waits for game.Workspace.HumanoidRootPart
to exist, not Game.Workspace.N4_3
.HumanoidRootPart`.
How do I fix my script that spawns a brick on the player?
Which player? The nearest player? A random player? All players?
-- gets one of the players in the game.
-- this is not a random player, it will give the same one if you run it again and noone has joined or left
local player = game.Players:GetPlayers()[1]
-- gets a random player in the game
local player = game.Players:GetPlayers()[math.random(game.Players.NumPlayers)]
-- does something to all players
for _,player in ipairs(game.Players:GetPlayers()) do
-- your code here
end
-- gets the nearest player to this part
local player = getNearestPlayer(script.Parent.Position)
function getNearestPlayer(pos)
local nearest = nil
local distanceMin = math.huge
-- loop through all players
for _,player in ipairs(game.Players:GetPlayers()) do
local root = player.Character and player.Character:FindFirstChild("HumanoidRootPart")
if root then -- if the player has a character and it's not missing the HumanoidRootPart
local distanceThis = (pos - root.Position).magnitude -- distance between the position and the HumanoidRootPart
if distanceThis < distanceMin then -- this one is closer than the last one
nearest = player
distanceMin = distanceThis
end
end
end
return nearest
end
Now we need “The Player”'s HumanoidRootPart.
local player = game.Players:GetPlayers()[1]
local TargetPart = player.Character.HumanoidRootPart
Let’s break that down.
player
is game.Players.N4_3
(if you are the only one in the game)
player.Character
is workspace.N4_3
player.Character.HumanoidRootPart
is workspace.N4_3.HumanoidRootPart
Your script should then look like this:
local player = game.Players:GetPlayers()[1]
local TargetPart = player.Character.HumanoidRootPart
local XValue = TargetPart.Position.X
local ZValue = TargetPart.Position.Z
script.Parent.Position = Vector3.new(XValue, 3.25, ZValue)
It can be made a bit shorter:
local TargetPosition = game.Players:GetPlayers()[1].Character.HumanoidRootPart.Position
script.Parent.Position = Vector3.new(TargetPosition.X, 3.25, TargetPosition.Z)
It’s prone to errors. It will write something red to the output if there is no player, or if there is a player and it is missing a character or its HumanoidRootPart. That’s fine. You can add a bunch of FindFirstChild
s or WaitForChild
s, but IMO it’s not critical.