Why does this script not work?

Hello,

I made these scripts to check if the value in the key matches, but the script works only for the query of the name and not the value. Could you help me, please?

local door = script.Parent.Door
local door2 = script.Parent.Door2
local hinge = script.Parent.Door.Hinge
local hinge2 = script.Parent.Door2.Hinge
local hingePos = hinge.Position
local hingePos2 = hinge2.Position
local open = false
local cooldown = false

script.Parent.Scanner1.Toch.Touched:Connect(function(hit)
	if hit.Parent.Level.Value == "5" then
		if open == false and cooldown == false then
			cooldown = true
			script.Parent.Scanner1.Lights.green2.Material = "Neon"
			for i = 1, 18, 1 do
				door:SetPrimaryPartCFrame(door:GetPrimaryPartCFrame(hingePos) * CFrame.new(0,0.2,0))
				door2:SetPrimaryPartCFrame(door2:GetPrimaryPartCFrame(hingePos2) * CFrame.new(0,-0.2,0))
				wait()	
			end
			script.Parent.Scanner1.Lights.green2.Material = "Metal"
			cooldown = false
			open = true
		elseif open == true and cooldown == false then
			cooldown = true
			script.Parent.Scanner1.Lights.green2.Material = "Neon"
			for i = 1, 18, 1 do
				door:SetPrimaryPartCFrame(door:GetPrimaryPartCFrame(hingePos) * CFrame.new(0,-0.2,0))
				door2:SetPrimaryPartCFrame(door2:GetPrimaryPartCFrame(hingePos2) * CFrame.new(0,0.2,0))
				wait()	
			end
			script.Parent.Scanner1.Lights.green2.Material = "Metal"
			wait(1)
			cooldown = false
			open = false
		end
	end
end)
2 Likes

Thank you for posting what you are trying to do in the post, but more details would also be helpful in the future. I’d suggest sharing a place/model file, or sharing errors that show up in the output window.

From the code given, I would suggest checking to see if “Level” exists before trying to operate on it.

local door = script.Parent.Door
local door2 = script.Parent.Door2
local hinge = script.Parent.Door.Hinge
local hinge2 = script.Parent.Door2.Hinge
local hingePos = hinge.Position
local hingePos2 = hinge2.Position
local open = false
local cooldown = false

script.Parent.Scanner1.Toch.Touched:Connect(function(hit)
	local levelTag = hit.Parent:FindFirstChild('Level')
	if levelTag and levelTag.Value == "5" and cooldown == false then
		cooldown = true
		if open == false then
			script.Parent.Scanner1.Lights.green2.Material = "Neon"
			for i = 1, 18, 1 do
				door:SetPrimaryPartCFrame(door:GetPrimaryPartCFrame(hingePos) * CFrame.new(0,0.2,0))
				door2:SetPrimaryPartCFrame(door2:GetPrimaryPartCFrame(hingePos2) * CFrame.new(0,-0.2,0))
				wait()	
			end
			script.Parent.Scanner1.Lights.green2.Material = "Metal"
			open = true
		elseif open == true then
			script.Parent.Scanner1.Lights.green2.Material = "Neon"
			for i = 1, 18, 1 do
				door:SetPrimaryPartCFrame(door:GetPrimaryPartCFrame(hingePos) * CFrame.new(0,-0.2,0))
				door2:SetPrimaryPartCFrame(door2:GetPrimaryPartCFrame(hingePos2) * CFrame.new(0,0.2,0))
				wait()	
			end
			script.Parent.Scanner1.Lights.green2.Material = "Metal"
			wait(1)
			open = false
		end
		cooldown = false
	end
end)
1 Like

Thank you for the help. I will test it today and then send a other message.

It don’t work anyway and it don’t output a error or something else.

No if I toch the door nothing will happen. That is the problem.

What part has the value? Is the value 5? Also, with changing materials, try using Enum.Material.[material name].

If you’re using a Number Value, don’t do
if hit.Parent.Level.Value == “5” then
Instead, do
if hit.Parent.Level.Value == 5 then

I think this should work, but if this was the case their would be error outputs. Only one way to find out!

1 Like

Thank you for this awnser. I was so dump to not check that! xD

1 Like

It’s alright. We always forget a thing or too like that sometimes. Np

I prefer using Enum.Material.Neon instead
image

1 Like