Hello, I am a beginner in scripting and wanted to create a simple system where a player can see which doors they can open. When the player approaches a door, a green highlight shines on it to indicate they are close enough to open it.
The issue I faced is that when I approach two doors, the highlight shines on both of them. I tried to solve this problem by using an array to keep track of which door is illuminated, but it didn’t work. I also attempted to use a Boolean value but that also failed.
Local Script :
local module = require(game.ReplicatedStorage.ModuleScript)
local doors = game.Workspace.Doors:GetChildren()
local player = game.Players.LocalPlayer
local char = player.Character
local humanoidrootpart = char:FindFirstChild("HumanoidRootPart")
local tool = script.Parent
local finalDistance = 0
local distance = 10
local toolEquipped = false
tool.Equipped:Connect(function()
toolEquipped = true
for i, v in ipairs(doors) do
if v:IsA("Model") then
local door = v.Door
module.highlight(v, 255, 0, 0)
local checkLoop = coroutine.wrap(function()
while wait(.15) do
finalDistance = (humanoidrootpart.Position - door.Position).Magnitude
if finalDistance <= 10 and toolEquipped then
module.highlight(v, 0, 255, 0)
elseif finalDistance > 10 and toolEquipped then
module.highlight(v, 255, 0, 0)
end
end
end)()
end
end
end)
tool.Unequipped:Connect(function()
toolEquipped = false
for i, v in ipairs(doors) do
module.highlightDel(v)
end
end)
local activated = false
tool.Activated:Connect(function()
for i, v in ipairs(doors) do
local highlightCheck = v:FindFirstChild("Highlight")
if highlightCheck and highlightCheck.FillColor == Color3.fromRGB(0, 255, 0) then
local door = v.Door
if not activated then
activated = true
door.Transparency = .5
task.wait(2)
door.Transparency = 0
activated = false
end
end
end
end)
Module Scirpt :
local module = {
highlight = function(v, r, g, b)
local highlight = Instance.new("Highlight")
highlight.FillColor = Color3.fromRGB(r, g, b)
highlight.OutlineColor = Color3.fromRGB(0, 0, 0)
local highlightClone = highlight:Clone()
local highlightCheck = v:FindFirstChild("Highlight")
if highlightCheck then
highlightCheck:Destroy()
highlightClone.Parent = v
else
highlightClone.Parent = v
end
end,
highlightDel = function(v)
local highlight = v:FindFirstChild("Highlight")
if highlight then
highlight:Destroy()
end
end,
}
return module