A way to find a character using a server script w/o using PlayerAdded or CharactcerAdded methods?

local array = {"Box"}
array[1] = workspace.tesst -- object name
local playerstuff = game.workspace:FindFirstChild(workspace.PlayersName.Value.Value):GetChildren()

local posX = script.Parent.Position.X
local posY = script.Parent.Position.Y
local posZ = script.Parent.Position.Z

local region = Region3.new(Vector3.new(posX-3, posY-1, posZ-3), Vector3.new(posX+3, posY+10, posZ+3))
while wait() do
for i,v in pairs( game.workspace:FindPartsInRegion3(region,nil,1)) do
if v == array[1] then
	script.Parent.BrickColor = BrickColor.Black()
elseif v ~=  array[1] then
	for i,p in pairs(playerstuff) do
		if v == p then
			print('doesnt change')
		else
	
	script.Parent.BrickColor = BrickColor.White()
	
	
end


end
end
end
end

in this script, I wish to make it so that if a certain brick goes onto a plate, the colour changes, and if the brick leaves the plate it reverts colour. I want to have it so if a player touches the plate it doesnt affect it at all, anyone know a way to do that?

EDIT: Sorry, i accidentally clicked the post button when I wasn’t finished with my question.

1 Like

There’s a few ways that you can “detect” a character. From my experience, I think the best way would be to check if the part in a model that has a humanoid and/or that model has the same name as a player in Players.

1 Like

Check if Documentation - Roblox Creator Hub with hit.Parent as argument returns a player or nil

3 Likes

Don’t do this. Just use Players:GetPlayerFromCharacter.

8 Likes

On a different note, you’re going to want a different interval on that while loop. Running chunky operations really quickly can cause lag.

1 Like

while wait() do always scares me when I see it. You almost never need something happening every ~0.03s. When you do, you’d likely use something more precise than wait().

1 Like

Yup, agreed. @Revelted, look into RunService.

RunService only if you need the precision. A longer wait if you don’t.

I don’t know if I’m reading the example correctly, or perhaps not understanding the goal, but that loop appears to set the plate to white for each non-character, non-test brick in the region, and black for the test brick. The resulting behavior doesn’t seem like it matches your described goal, in that it’s not the black-setting brick leaving the region that sets the color back, it requires the presence of another part (which is not a character part) to set it to white.

Furthermore, if both black-setting and white-setting parts are in the region, the result is left to the ordering of the results returned by FindPartsInRegion3. Even with the optimization of adding break statements after each place where brick color is set, it’s still going to be dependent on this ordering–just respecting the first meaningful block encountered during iteration, rather than last.

Next up is the issue of playerstuff being the children of what I’m assuming is the character model. This includes only the first generation children, so something like a “Handle” part in a player’s accessory is still going to count as something that can set the plate white, unless…

…you use FindPartsInRegion3WithIgnoreList and pass the character in the ignore list. This will ensure all descendants of the character model will be absent from the results. This is probably good for one character, but probably bad if you pass ALL player characters on a multi-player server as the ignore list (I haven’t tried this). You may have to do a broadphase triage to only ignore characters overlapping the region.

If speed is important, and you really have to check against potentially all characters on a server, your best bet might be to maintain a table of all baseParts of characters, so that you have O(1) lookup to know if a part is part of a character. Obviously you’d need support code to maintain that map which would necessarily involve at least CharacterAdded, CharacterAppearanceLoaded or some ChildAdded handlers.

The poster is asking how to get a player’s character and thus Players:GetPlayerFromCharacter() wouldn’t work.

With all due respect, I actually cannot read their code due to the way it’s formatted.

If you want a player’s character by their name, you can do Players:FindFirstChild(name).Character. Is this what you want?

In my experience, I never have any reason to do a “while true do” loop. MAYBE for something like “while loading” but even then I never do that. Are you rendering a Gui animation or maybe a part animation, like cframing a part? That goes in RunService.RenderStepped, because you want to do something for every frame that is rendered.

I’ve only ever used a while true do loop for stuff like handling rounds to make sure they loop. Like I said before I can’t read the code in the OP because of the formatting, so I don’t really know what they’re trying to do.

CollectionService may be the easiest way to manage this, since you don’t need to worry about removing character’s baseparts from the table when they’re no longer relevant (when they’re destroyed).

I’ve figured out the solution to the problem ^^ thanks everyone!

(P.s : im a very new scripter, so sorry for my messy scripting >.<)

I sometimes use

workspace:WaitForChild( playerName );

for getting their character.

Don’t.

local player = Players:FindFirstChild(playerName)
local character = player.Character or player.CharacterAdded:Wait()
3 Likes