KeyCardSystem Breaking

I have this script that is supposed to be able to turn on and off the lights and also pause and unpause a different models scripts. This was all working until I tried making it so if the player is holding out a keycard then the script would run.


Any ideas?

Are you able to put the code into a preformatted text? It makes it more readable.
Also you can remove the whole function for checking if the players holding a keycard and just do if not player.Character:FindFirstChild("MaintenaceKeycard") then return end

Like the dude said above, you can do that. Or you can just first do…

local keycard = isHoldingKeyCard(player)

change the isHoldingKeyCard() function from…

local function isHoldingKeyCard(player)

to;

isHoldingKeyCard = function(player)

and then do;

local keycard = isHoldingKeyCard(player)

if not keycard then return end
local prox = script.Parent

local lightStates = {}


isHoldingKeyCard = function(player)
	local keycard = isHoldingKeyCard(player)

	if not keycard then return end
			end
		
	


local function toggleLights(state)
	for _, obj in pairs(game.Workspace:GetDescendants()) do
		if obj:IsA("PointLight") or obj:IsA("SurfaceLight") or obj:IsA("SpotLight") then
			if state then
				if lightStates[obj] then
					obj.Brightness = lightStates[obj].Brightness
					obj.Range = lightStates[obj].Range
					obj.Color = lightStates[obj].Color
					obj.Enabled = lightStates[obj].Enabled
				end
			else
				if not lightStates[obj] then
					lightStates[obj] = {
						Brightness = obj.Brightness,
						Range = obj.Range,
						Color = obj.Color,
						Enabled = obj.Enabled
					}
				end
				obj.Brightness = 0 
				obj.Enabled = false
			end
		end
	end
end

local function setScriptsState(model, state)
	if model then
		for _, obj in pairs(model:GetDescendants()) do
			if obj:IsA("Script") or obj:IsA("LocalScript") then
				obj.Disabled = state
			end
		end
	end
end

prox.Triggered:Connect(function(player)

	if not isHoldingKeyCard(player) then
		return 
	end

	local part = script.Parent.Parent.Parent:FindFirstChild("Part")
	local generator = game.Workspace:FindFirstChild("generatorAHAH")
	if part then
		if part.BrickColor == BrickColor.new("Lime green") then
			part.BrickColor = BrickColor.new("Really red")
			toggleLights(false) 
			setScriptsState(generator, true) 
		elseif part.BrickColor == BrickColor.new("Really red") then
			part.BrickColor = BrickColor.new("Lime green")
			toggleLights(true) 
			setScriptsState(generator, false)
		end
	end
end)

I tried it like this, and it still didnt work. What else might be incorrect?

It kinda just gets really laggy whenever i trigger the proximity prompt now

It didn’t work because you put the script parts in the wrong spots.

Let me rewrite this…

local prox = script.Parent

local lightStates = {}


isHoldingKeyCard = function(player)
       local character = player.Character
       if character then
--NEW
              local tool = character:FindFirstChildWhichIsA("Tool")
              if tool and tool.Name == "MaintenanceKeyCard" then return true end
--/NEW
              return false
       end
end
--i made it so that you dont need to loop over the entire player's character
	


local function toggleLights(state)
	for _, obj in pairs(game.Workspace:GetDescendants()) do
		if obj:IsA("PointLight") or obj:IsA("SurfaceLight") or obj:IsA("SpotLight") then
			if state then
				if lightStates[obj] then
					obj.Brightness = lightStates[obj].Brightness
					obj.Range = lightStates[obj].Range
					obj.Color = lightStates[obj].Color
					obj.Enabled = lightStates[obj].Enabled
				end
			else
				if not lightStates[obj] then
					lightStates[obj] = {
						Brightness = obj.Brightness,
						Range = obj.Range,
						Color = obj.Color,
						Enabled = obj.Enabled
					}
				end
				obj.Brightness = 0 
				obj.Enabled = false
			end
		end
	end
end

local function setScriptsState(model, state)
	if model then
		for _, obj in pairs(model:GetDescendants()) do
			if obj:IsA("Script") or obj:IsA("LocalScript") then
				obj.Disabled = state
			end
		end
	end
end

prox.Triggered:Connect(function(player)
--NEW
	local keycard = isHoldingKeyCard(player)

        if not keycard then return end
--/NEW
	local part = script.Parent.Parent.Parent:FindFirstChild("Part")
	local generator = game.Workspace:FindFirstChild("generatorAHAH")
	if part then
		if part.BrickColor == BrickColor.new("Lime green") then
			part.BrickColor = BrickColor.new("Really red")
			toggleLights(false) 
			setScriptsState(generator, true) 
		elseif part.BrickColor == BrickColor.new("Really red") then
			part.BrickColor = BrickColor.new("Lime green")
			toggleLights(true) 
			setScriptsState(generator, false)
		end
	end
end)
1 Like