As you said this will set the parts then set it back so only the last player will define what is set. A simple way to resolve this would to use break which will exit out of the loop it was called in. You would just put the break in your if statment for when the distance is < 10.
There are some more miner points with your code. You should create a variable that holds the part as it is used often. You should also be using Enum and not a string as it is good practice. Lastly I would inc the delay time for the loop as it will help a lot in normal gameplay.
Example
local plrServ = game:GetService("Players")
local part = workspace.Part
while true do
wait(0.3)
for _, plr in pairs(plrServ:GetPlayers()) do
if plr:DistanceFromCharacter(part.Position) < 10 then
break
else
end
end
end
This is one possible solution but you really only want to be setting the parts brick color and material when you need it. For this a function would work better.
local plrServ = game:GetService("Players")
local part = workspace.Part
local function changePart()
for _, plr in pairs(plrServ:GetPlayers()) do
if plr:DistanceFromCharacter(part.Position) < 10 then
return true
end
end
return false
end
while true do
wait(0.3)
if changePart() then
else
end
end
This can also be improved if you want to use a boolean variable to keep track of if the part is red or not to find if there is any need to change the part ie if the part is red then we do not need to change it to red.
I hope this helps. please comment if you have any questions.
4 comments
You could try using Region3
You could try using a variable to detect if a player's in the distance.
So what you want if a player is within a proximity of a block the block will turn red?
^ Pretty much. But it turns black as theres players OUTSIDE the area aswell as inside. I will look into region3 and the variables.