Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

DistanceFromCharacter script not working as intended?

while wait() do
	local Players = game.Players:GetPlayers()
	for i, v in pairs(Players) do
		print("PLAYER: "..v.Name.." DISTANCE: "..v:DistanceFromCharacter(workspace.Part.Position))
		if v:DistanceFromCharacter(workspace.Part.Position) < 10 then
			workspace.Part.BrickColor = BrickColor.new("Really red")
			workspace.Part.Material = "Neon"
		else
			workspace.Part.BrickColor = BrickColor.new("Really black")
			workspace.Part.Material = "Plastic"
		end
	end
	
end

So, I know what the problem is. Since other players are outside the area, it thinks it should turn black. But, how do I make it so, if even one player is in the area, it turns red. Even when other players are outside? Thank you in advance.

4 comments

0

You could try using Region3

0

You could try using a variable to detect if a player's in the distance.

0

So what you want if a player is within a proximity of a block the block will turn red?

0

^ Pretty much. But it turns black as theres players OUTSIDE the area aswell as inside. I will look into region3 and the variables.

2 answers

✓ Accepted answer

The problem here is that you are trying to change the color of the block after you check the distance of each player to the block. So, it will change the block color according to the distance of the last person checked on the loop. You need to turn the block's color after checking each player's distance from the block. You can put the minimum distance on a variable.

while true do
    local Players = game.Players:GetPlayers()
    --Minimum distance.
    local minimum = 10
    for i, v in pairs(Players) do
		local distance = v:DistanceFromCharacter(workspace.Part.Position)
        print("PLAYER: "..v.Name.." DISTANCE: "..distance)
        if distance < minimum then
			minimum = distance
		end
    end
	--Check if minimum distance is less than the threshold (10).
	if minimum < 10 then
        workspace.Part.BrickColor = BrickColor.new("Really red")
        workspace.Part.Material = "Neon"
	else
        workspace.Part.BrickColor = BrickColor.new("Really black")
        workspace.Part.Material = "Plastic"
	end
	wait()
end

1 comment

0

Thank you.

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") -- players service is used a lot
local part = workspace.Part

while true do -- it is best not to use while wait() do 
	wait(0.3) 

	for _, plr in pairs(plrServ:GetPlayers()) do
		if plr:DistanceFromCharacter(part.Position) < 10 then
			-- ect
			break -- exit out of the for loop and go back to the while loop
		else
			-- ect
		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") -- players service is used a lot
local part = workspace.Part

-- using return will stop the loop as well as return the value passed if < 10
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 -- call the function
		-- change to red
	else
		-- change to black
	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.

5 comments

0

Two questions: What is wrong with using while wait() do?

0

And the for _, plr in pairs, is that the same as for i, v in pairs?

0

it exploits the fact that it returns a value which is truthy. As you know that the while loop will run all the time it is best to have a constant value and that while true do is more readable.

0

for _, plr == for i, v in pairs. _ is commonly used a a place holder for things that we do not care about. Also I try to name variable where possible. You end up with things like v, v2, v3 ..

0

Ah, thank you. You're a savior.

This discussion is preserved as it was. New answers, comments, and votes are closed.