Hello, I’m trying to touch 3 parts to open a door. All 3 Parts need to be touched for the door to open. The error I get is in line 8 which says “Attempt to call a nil value”. What is wrong in line 8?
local part1 = workspace.Part1
local part2 = workspace.Part2
local part3 = workspace.Part3
local touchedPart1 = false
local touchedPart2 = false
local touchedPart3 = false
local player = GetService(Players).LocalPlayer
local character = player.CharacterAdded:Wait()
player.CharacterAdded:Connect(function(char)
character = char
end)
local door = workspace.Door
part1.Touched:Connect(function(hit)
if hit.Parent == character then
partTouched1 = true
end
if partTouched2 and partTouched3 then
door.Transparency = 0.5
door.CanCollide = false
end
end)
part2.Touched:Connect(function(hit)
if hit.Parent == character then
partTouched2 = true
end
if partTouched1 and partTouched3 then
door.Transparency = 0.5
door.CanCollide = false
end
end)
part3.Touched:Connect(function(hit)
if hit.Parent == character then
partTouched3 = true
end
if partTouched2 and partTouched3 then
door.Transparency = 0
door.CanCollide = false
end
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
player.CharacterAdded:Connect(function(char)
character = char
end)
This is my new script when I add that part. Hopefully I added the right part in the right place as I get this " attempt to index nil with ‘Character’"
local part1 = workspace.Part1
local part2 = workspace.Part2
local part3 = workspace.Part3
local touchedPart1 = false
local touchedPart2 = false
local touchedPart3 = false
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
player.CharacterAdded:Connect(function(char)
character = char
end)
local door = workspace.Door
part1.Touched:Connect(function(hit)
if hit.Parent == character then
partTouched1 = true
end
if partTouched2 and partTouched3 then
door.Transparency = 0.5
door.CanCollide = false
end
end)
part2.Touched:Connect(function(hit)
if hit.Parent == character then
partTouched2 = true
end
if partTouched1 and partTouched3 then
door.Transparency = 0.5
door.CanCollide = false
end
end)
part3.Touched:Connect(function(hit)
if hit.Parent == character then
partTouched3 = true
end
if partTouched2 and partTouched3 then
door.Transparency = 0
door.CanCollide = false
end
end)
An easier approach would be to put all of the Parts inside a Folder, then get them using the GetChildren() function which will return back a table, then you can detect what parts have been Touched & what hasn’t, then check through a loop if all of the PartsRemaining are equal to 0:
local Parts = workspace.PartFolder
local Door = workspace.Door
local RunService = game:GetService("RunService")
local PartsRemaining = #Parts --Or 3
for _, Part in pairs(Parts:GetChildren()) do
local PartDebounce = false
Part.Touched:Connect(function(Hit)
local HitPlayer = game.Players:GetPlayerFromCharacter(Hit.Parent)
if HitPlayer and not PartDebounce then
PartDebounce = true
PartsRemaining -= 1
end
end)
end
while true do
if PartsRemaining == 0 then
Door.Transparency = 0.5
Door.CanCollide = false
break
end
RunService.RenderStepped:Wait()
end
Also if this isn’t in a LocalScript like@aquawrspmentioned I swear I’m gonna break 500 keyboards