Workspace.67kraccbacc.CutToolRed.LocalScript:89: attempt to index nil with 'InputBegan' - Client - LocalScript:89

so i have a slicing script and it uses a tool to slice stuff its like a semi laser and when i use it it works the first time secondly i cant use it and i try to re equip the tool and the console says Workspace.67kraccbacc.CutToolRed.LocalScript:89: attempt to index nil with ‘InputBegan’ - Client - LocalScript:89

heres the script btw

–services

local playersService = game:GetService(“Players”)
local userInputService = game:GetService(“UserInputService”)
local runService = game:GetService(“RunService”)

–tool things

local tool = script.Parent
local target = nil

–inputs

local connectionInputBegan = nil
local connectionInputEnded = nil
local connectionLoop = nil
local userInput = nil

–camera type

local cameraType = nil

–position

local serverPosition = nil

–raycast stuff

local rayInstance = nil
local rayStartPosition = nil
local rayPreviousPosition = nil
local raycastParams = RaycastParams.new()

–functions

local function Raycast()
local mouse = userInputService:GetMouseLocation()

--raycasts

local ray = game.Workspace.CurrentCamera:ViewportPointToRay(mouse.X, mouse.Y)
local result = workspace:Raycast(ray.Origin, ray.Direction * 100, raycastParams)
if result == nil then
	return ray.Origin + ray.Direction * 100
else
	return result.Position, result.Instance
end

end

local function Loop(deltaTime)
local position, instance = Raycast()

target.Position = position

--making on the server not send so much events

if (position - serverPosition).Magnitude > 2 then
	serverPosition = position
	tool.PositionEvent:FireServer(serverPosition)
end

--raycast stuff

if rayInstance ~= instance then
	if rayInstance ~= nil and rayInstance.Anchored == false and rayInstance:IsDescendantOf(game.Workspace.Slice) == true then
		local center = (rayStartPosition + rayPreviousPosition) / 2
		local vectorX = (rayPreviousPosition - rayStartPosition).Unit
		local vectorZ = (game.Workspace.CurrentCamera.CFrame.Position - center).Unit
		local vectorY = vectorZ:Cross(vectorX)
		local cFrame = CFrame.fromMatrix(center, vectorX, vectorY)
		tool.SliceEvent:FireServer(rayInstance, cFrame)
	end
	rayInstance = instance
	rayStartPosition = position
end
rayPreviousPosition = position

end

– mouse or touch detected or ended

local function InputEnded(input, proccesed)

--if no input then quickly stop the code

if userInput ~= input then return end

--reverts to the normal camera type

if cameraType ~= nil then
	game.Workspace.CurrentCamera.CameraType = cameraType
	cameraType = nil
end

-- yeah i dont know either

userInputService = nil
rayInstance = nil
connectionInputEnded:Disconnect()
connectionLoop:Disconnect()
tool.PositionEvent:FireServer(nil)

end

local function InputBegan(input, proccesed)

--if no input same aging return to end

if userInput ~= nil then return end

--fixes a bug when hovering on a gui for example the tool gui its still activated

if proccesed == true then return end
if input.UserInputType ~= Enum.UserInputType.MouseButton1 and input.UserInputType ~= Enum.UserInputType.Touch then return end
if input.UserInputType == Enum.UserInputType.Touch then
	local guis = playersService.LocalPlayer.PlayerGui:GetGuiObjectsAtPosition(input.Position.X, input.Position.Y)
	for i, gui in ipairs(guis) do
		if gui.Name == "DynamicThumbstickFrame" then return end
	end
	cameraType = game.Workspace.CurrentCamera.CameraType
	game.Workspace.CurrentCamera.CameraType = Enum.CameraType.Fixed
end
userInput = input
serverPosition = Raycast()
tool.PositionEvent:FireServer(serverPosition)
target = tool:WaitForChild("Target")
connectionInputEnded = userInputService.InputEnded:Connect(InputEnded)
connectionLoop = runService.Heartbeat:Connect(Loop)

end

tool.Equipped:Connect(function()

-- filters the character

raycastParams.FilterDescendantsInstances = {tool.Parent}
connectionInputBegan = userInputService.InputBegan:Connect(InputBegan)

end)

tool.Unequipped:Connect(function()

--stops the input began thing

connectionInputBegan:Disconnect()

end)

You should probably improve the formatting of your code.

2 Likes

i dont understand you like what do i have to improve

I think he means like this

```lua
-- put some code here
```

and it becomes this:

-- put some code here
4 Likes

The code is impossible to read. Please, format it properly. We can’t help you anyhow right now.

3 Likes

I showed how to format codeblocks :wink:

1 Like