How do I fix my script that spawns a brick on the player?

Hmm @2147483647, try this instead [not fully aware on how to get a players model from workspace]:

local workspace = game:GetService("Workspace")
local TargetPart = workspace.LocalPlayer:WaitForChild("HumanoidRootPart")
local XValue = TargetPart.Position.X
local ZValue = TargetPart.Position.Z

script.Parent.Position = Vector3.new(XValue, 3.25, ZValue)

If this doesn’t work, try replacing what is currently stored in the workspace variable to simply game.Workspace.

… This won’t work either chief.

Is it the local workspace? Couldn’t remember if it’s a service or not.

The entire script is incorrect, you need to find a way to grab the player that is in the “bossfight” and then use the players Character to get their HumanoidRootPart.

The script he attached was:

All I can think of is adding .LocalPlayer next to game.Workspace. @2147483647 please tell me if this works.

That doesn’t even make sense, if you don’t know how to script, please don’t confuse him more.

I do know how to script a little bit, I’m trying to help in the best possible way.

Doing what I usually do, i’ll try to help you.
First of all, I like to use CFrame and I’m not very good at Positions but ill try

I understand you are using a Localscript? Correct me if I’m wrong.
Currently, you do not have the player.

Get the player in a local script by doing the following:
local plr = game.Players.LocalPlayer`
This only works in a local script.

So if “HumanoidRootPart” is the players humanoid, then do the following

local plr = game:GetService("Players").LocalPlayer
local TargetPart = plr.Character:WaitForChild("HumanoidRootPart")

Now for the position, I HAVE NOT tried this myself, but I think it would work if you just change that in so the result would be:

local plr = game:GetService("Players").LocalPlayer
local TargetPart = plr.Character:WaitForChild("HumanoidRootPart")
local XValue = TargetPart.Position.X
local ZValue = TargetPart.Position.Z

script.Parent.Position = Vector3.new(XValue, 3.25, ZValue)

I’m still kind of confused on what you are trying to create with the script.Parent.Position but I’ve tried my best to help!

He’s trying to do this on the server, LocalPlayer doesn’t exist on the server…

@JustNode Informed me this was on the server.

See, you can have 2 options, one would be to do this when the player joins using the event “PlayerAdded” and the second one would be to make a way for this to activate, like clicking a button.

If you wish to use playeradded, use the following command and remember the first argument is the player-object

game.Players.PlayerAdded:Connect(function(plr)
 local TargetPart = plr.Character:WaitForChild("HumanoidRootPart")
 local XValue = TargetPart.Position.X
 local ZValue = TargetPart.Position.Z

 script.Parent.Position = Vector3.new(XValue, 3.25, ZValue)
end)

If you are choosing the activation method, you need a way to activate it, like a button. The example right now is a button, using a ClickDetector of course. You first need to find the ClickDetectors events, The event for a clicked button is “MouseClick” So it will look like this:

local button = path  -- remove path and put in the path for the button. example: "game.Workspace.Button"
button.ClickDetector.MouseClick:Connect(function(plr) -- first argument is still the player-object
 local TargetPart = plr.Character:WaitForChild("HumanoidRootPart")
 local XValue = TargetPart.Position.X
 local ZValue = TargetPart.Position.Z

 script.Parent.Position = Vector3.new(XValue, 3.25, ZValue)
end)

First, this results in another error like this. Second, I need to make sure this works at a certain time frame since it’s well, a boss. The click detector script I don’t think would be useful for this situation.

image

If you want to do it at a certain frame, the thing I would do in that situation is to use PlayerAdded event and then go off of that, that way I’ll always have the player. And when It’s time, you execute the script. I’m unsure how else I can help you :frowning:

Oh alright, I’ll see what I can do with that.

1 Like

You can also use remotes to grab the player in an event.

1 Like

Yes, that’s also a very good way to get the player once it’s required, totally flew by my mind.

Look, there’s another way to do this, you can make a RemoteEvent fires when you want the boss battle to be done.

And then do a for i, v loop taking all the players that are in the game. And then take their character. Like:

for i, v in pairs(game.Players:GetPlayers())
       local character = v.Character
       local TargetPart = character:WaitForChild("HumanoidRootPart")
       local XValue = TargetPart.Position.X
       local ZValue = TargetPart.Position.Z

       script.Parent.Position = Vector3.new(XValue, 3.25, ZValue)
end
1 Like

local players = game:GetService(“Players”)
repeat wait()
until players.LocalPlayer.Character
local TargetPart = players.LocalPlayer:WaitForChild(“HumanoidRootPart”)
local XValue = TargetPart.Position.X
local ZValue = TargetPart.Position.Z

script.Parent.Position = Vector3.new(XValue, 3.25, ZValue)

This’ll not work, normal scripts do not support LocalPlayer

ohhh, normal script dident realize

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 FindFirstChilds or WaitForChilds, but IMO it’s not critical.

1 Like