Scripting Errors

I get this error:

function: 0xd48f6b71723e4879

And this is the script:

local EMF_Zones = workspace:FindFirstChild("EMF_Zones")
local EMF_Lights = script.Parent:WaitForChild("Lights")
local Tool = script.Parent
local Tool_Handle = Tool:WaitForChild("Handle")
local EMF_On = false

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local Character = Player.Character or Player.CharacterAdded:Wait()
local Event = script.Parent:WaitForChild("Event", 0.5)or Instance.new("RemoteEvent", script.Parent).Name == "Event"

local InputP = game:GetService("UserInputService")

local function CheckTouchingParts(Parts)
	for Numver, Object in pairs(Parts) do
		if Object:IsDescendantOf(EMF_Zones) then
			return Object:FindFirstChild("EMF_Reading").Value
		end
	end
	return 1
end

InputP.InputBegan:Connect(function(Input, Value)
	if Input.UserInputType == Enum.UserInputType.MouseButton2 and script.Enabled.Value == true and EMF_On == false then
		Event:FireServer(EMF_Lights, 1)
		local EMF_Reading = CheckTouchingParts(Tool_Handle:GetTouchingParts())
		if EMF_Reading > 1 then
			Event:FireServer(EMF_Lights, EMF_Reading)
		end
		EMF_On = true
	elseif Input.UserInputType == Enum.UserInputType.MouseButton2 and script.Enabled.Value == true and EMF_On == true then
		Event:FireServer(EMF_Lights, 0)
		EMF_On = false
	end
end)

local function OnTouch(Hit)
	if Hit and Hit.Parent and Hit:IsDescendantOf(EMF_Zones) and EMF_On == true then
		local EMF_Reading = CheckTouchingParts(Tool_Handle:GetTouchingParts())
		Event:FireServer(EMF_Lights, EMF_Reading)
		print(error)
	end
end

function OnNoTouch()
	if EMF_On == true then
		Event:FireServer(EMF_Lights, 1)
	end
end

Tool_Handle.Touched:Connect(OnTouch)
Tool_Handle.TouchEnded:Connect(OnNoTouch)

Tool.Equipped:Connect(function()
	script.Enabled.Value = true
end)

Tool.Unequipped:Connect(function()
	script.Enabled.Value = false
end)

The part that gives the error:

function OnTouch(Hit)
	if Hit and Hit.Parent and Hit:IsDescendantOf(EMF_Zones) and EMF_On == true then
		local EMF_Reading = CheckTouchingParts(Tool_Handle:GetTouchingParts())
		Event:FireServer(EMF_Lights, EMF_Reading)
		print(error)
	end
end

That’s because error is a function, are you trying to make your script error? If so, you can just call error as you’d call a normal function (error(errorMessage)). If you’re trying to catch an error, you can use pcall.

local success, EMF_Reading = pcall(CheckTouchingParts, Tool_Handle:GetTouchingParts())
if not success then
    warn(EMF_Reading) -- EMF_Reading would be the error message if it were to error.
end

[quote=“7z99, post:2, topic:1384576”]

local success, EMF_Reading = pcall(CheckTouchingParts, Tool_Handle:GetTouchingParts())
if not success then
    warn(EMF_Reading) -- EMF_Reading would be the error message if it were to error.
end

Okay.

Just a little tip about when using the FindFirstChild method:

local ItExists = Object:FindFirstChild("LookingFor") 

if ItExists then
  -- We are sure it exists
end 

You might wanna check if it exists with a little if block.

This doesn’t print ANY error. But, my lights wouldn’t change material.

Script:

local Event = script.Parent:WaitForChild("Event", 0.5)or Instance.new("RemoteEvent", script.Parent).Name == "Event"

Event.OnServerEvent:Connect(function(plr, EMF_Lights, EMF_Value)
	if EMF_Value > 0 then
		for Number, Object in pairs(EMF_Lights:GetChildren()) do
			EMF_Lights["Light".. Number].Material = Enum.Material.Glass
			EMF_Lights["Light".. Number].Transparency = 0.5
			local success, EMF_Reading = pcall()
			if not success then
				warn(EMF_Reading)
			end
		end
		for i = 1, EMF_Value do
			EMF_Lights["Light".. EMF_Value].Material = Enum.Material.Neon
			EMF_Lights["Light".. EMF_Value].Transparency = 0
			
			local success, EMF_Reading = pcall()
			if not success then
				warn(EMF_Reading)
			end
		end
		for Number, Object in pairs(EMF_Lights:GetChildren()) do
			EMF_Lights["Light".. Number].Material = Enum.Material.Glass
			EMF_Lights["Light".. Number].Transparency = 0.5
			local success, EMF_Reading = pcall()
			if not success then
				warn(EMF_Reading)
			end
		end
	end
end)

Error to this: attempt to call a Instance value

You aren’t using pcall properly, pcall requires your function and any arguments to be passed.

local success, EMF_Reading = pcall(CheckTouchingParts, Tool_Handle:GetTouchingParts())
1 Like