Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Door Open Depending on Magnitude?

Asked by 9 years ago

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

Log in to vote
0
Answered by 9 years ago
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.

Ad

Answer this question