So earlier today I finished making my keycard door script. The cards are supposed to be able to open doors that have the same level as it and less. I’m doing this by storing a “level” value in the door and a “level” value in the key (tool). It’s opening the doors fine and there’s no issues with that, but when I try to implement a sound for when the key isn’t high enough level, it starts glitching out. I’ve noticed a pattern with it where if I open a door that’s a higher level than the key, it will play the sound for the rest of the keys whenever you open any door until you try to open another door that’s level is too high.
The script:
local player = game.Players.LocalPlayer
local character = player.Character
if not character or not character.Parent then
character = player.CharacterAdded:wait()
end
local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local TweenPlaying
local RemoteEvent
local Level
local CardLevel = script.Parent.CardLevel
local tool = script.Parent
local Equipped = false
local BillboardGui
local Errorsound = script.Parent.ErrorSound
--if equipped
tool.Equipped:Connect(function()
Equipped = true
end)
--if unequipped
tool.Unequipped:Connect(function()
Equipped = false
end)
local RegionSize = 10
local function MakeRegion(Pos)
return Region3.new(Pos - Vector3.new(1,1,1)*(RegionSize/2), Pos + Vector3.new(1,1,1)*(RegionSize/2))
end
function GetPart()
if Equipped == true then
local Reg = MakeRegion(character.HumanoidRootPart.Position)
local Parts = workspace:FindPartsInRegion3(Reg,character,math.huge) --ignore parts from the character
for _,Part in pairs(Parts) do
if Part.Parent.Name == "DoorL0" or "DoorFrame" then
if (Part.Position - character.HumanoidRootPart.Position).magnitude <= RegionSize/2 then
TweenPlaying = Part.Parent.Parent.TweenPlaying
RemoteEvent = Part.Parent.Parent.DoorL0Event
Level = Part.Parent.Parent.Level
Part2 = Part
BillboardGui = Part.Parent.Parent.BillboardGui
end
end
end
end
end
UIS.InputBegan:connect(function(input)
if input.UserInputType == Enum.UserInputType.Keyboard then
if input.KeyCode == Enum.KeyCode.E then
GetPart()
if TweenPlaying ~= nil then
if CardLevel.Value >= Level.Value then
if TweenPlaying.Value == false and Equipped == true then
print(Part2, RemoteEvent, TweenPlaying)
RemoteEvent:FireServer()
RemoteEvent = nil
Part2 = nil
TweenPlaying = nil
end
elseif CardLevel.Value < Level.Value then
Errorsound:Play()
end
end
end
end
end)
The problem:
elseif CardLevel.Value < Level.Value then
Errorsound:Play()
end
Thanks