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

Door Open Depending on Magnitude?

I'm working on a car garage in my game, and I want it so if any player is within 20 studs of the door, the brick is invisible, non-can-collide, and the decals are invisible. As of now, it works perfectly on single-player. But when multiple players are within the 20 stud radius, it breaks, permanently staying closed. What should I change to this existing script to make it work for multiple players?

while true do
	local door = script.Parent
	for i,v in pairs(game.Players:GetChildren()) do
		if (v.Character.Torso.Position - door.Position).magnitude <= 20 then
			door.CanCollide = false
			door.Transparency = 1
			door.Decal.Transparency = 1
			door.Decal2.Transparency = 1
		else
			door.CanCollide = true
			door.Transparency = 0
			door.Decal.Transparency = 0
			door.Decal2.Transparency = 0
		end
	end
	wait(0.3)
end

1 answer

✓ Accepted answer
while true do
    local door = script.Parent
local open = false
    for i,v in pairs(game.Players:GetChildren()) do
        if (v.Character.Torso.Position - door.Position).magnitude <= 20 then
            door.CanCollide = false
            door.Transparency = 1
            door.Decal.Transparency = 1
            door.Decal2.Transparency = 1
open = true
end
end
if open == false then
                  door.CanCollide = true
            door.Transparency = 0
            door.Decal.Transparency = 0
            door.Decal2.Transparency = 0
    end
    wait(0.3)
end

the error in your script is it closes and opens on every person so if the last person isnt close enough it closes.

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